Article View: gnu.g++.help
Article #874Argument matching
From: Stefan Neis
Date: Thu, 11 Jan 1996 15:43
Date: Thu, 11 Jan 1996 15:43
79 lines
3099 bytes
3099 bytes
Hi there,
I'm having an ugly problem with templates and inheritance. I mirrored
my problem by a less complicated --though useless -- example which
avoids using templates. Assume the following class hierarchie:
A B
|
Bderived
and let there be a constructor which makes a Bderived from A.
I hoped, the compiler would automatically convert A to Bderived (user
defined conv.) and then convert this to B (standard conversion, isn't
it?) if I give an "A" as argument to a function expecting a "B". But
this is obviously not the case -- neither with g++ nor with CC -- (see
the following example), so I assume that a user defined conversion
always has to be the last conversion that is applied to an
argument. Is this assumption correct (Couldn't find anything on this
in Stroustrup's "The C++ Programming Language")? If so, are there
plans to change this? I would find this kind of conversions extremely
useful for using templates, e.g. (to get to my original example) if
you want a "vector<int>" and a "vector<complex>", where complex is
intended to be some class realizing complex numbers, you would like to
have a automatic conversion from "vector<int>" to "vector<complex>"
and maybe "vector<complex>" should have additional functions. So I
wrote a "base_vector<T>" (corresponding to B) where all common
functions are included, vector inherits from this, and is specialised
for complex (corresponding to Bderived) and now the automatic
conversion from vector<int>(corresponding to A) to vector<complex>
doesn't work as I hoped it to do. Any Suggestions on how to circumvent
this problem? Is my problem really that unusual?
Stefan Neis
P.S.: I suppose, I could additionally define a "cast operator" from A to
the base_class B (constructor won't do, since B is intended
to be a template class...), but if I would derive Bderived by
several steps from B this would lead to programming the "same"
cast operator over and over again....
--------------------- EXAMPLE -----------------------------------
class A
{
int i;
public:
A(){}
A(int x){i=x;}
int get() const {return i;};
~A(){}
};
class B
{
long i; // using long instead of "complex" to get
// something that compiles more easily.
public:
B(){}
B(int x){i=x;}
~B(){}
friend B operator * (const B&, const B&);
};
class Bderived: public B
{
public:
Bderived():B(){}
Bderived(int x):B(x){}
Bderived(const A & x):B(x.get()){}
~Bderived(){}
};
main()
{ A testA(4);
Bderived testBder(5);
B testB;
testB = Bderived(testA) * testBder; // this -- of course -- does work.
testB = testA * testBder; // this won't compile at all.
}
Technical Details
Message-ID:
<9601111542.AA05990@crypt5.cs.uni-sb.de>Path:
arc1-ca.pugleaf.net!cs.uni-sb.de!neisShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!cs.uni-sb.de!neis From: neis@cs.uni-sb.de (Stefan Neis) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Argument matching Date: 11 Jan 1996 10:43:42 -0500 Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu Sender: daemon@cis.ohio-state.edu Distribution: world Message-ID: <9601111542.AA05990@crypt5.cs.uni-sb.de>