overriding derived class copy constructor

Recommended Posts

Timkin    864
Assume we have two base classes:
struct Base_foo1{
int x, y;
};

struct Base_foo2{
int u, v;
};

and a derived class with multiple inheritance
class Derived_foo : public Base_foo1, Base_foo2{}

Now, obviously, I could overload the default copy constructor for Derived_foo in the following way:
Derived_foo::Derived_foo(const Derived_foo& rhs){
x=rhs.x;
y=rhs.y;
u=rhs.u;
v=rhs.v;
}

However, I'd like to know if there is a way to use an initialiser list for this overloaded copy constructor in a multiple inheritance scenario. Obviously, in a single inheritance scenario its fairly trivial...
class Derived_foo1{
Derived_foo1(const Derived_foo1& rhs):Base_foo1(rhs){}
}

...but I just cant see the solution in the former case. Is there one? Thanks in advance, Timkin

Share on other sites
SiCrane    11839
Huh? Is there any reason you aren't using:
class Derived_foo1{Derived_foo1(const Derived_foo1& rhs):Base_foo1(rhs),Base_foo2(rhs){}}

Share on other sites
Timkin    864
Quote:
 Original post by SiCraneHuh? Is there any reason you aren't using:

well, since you asked... probably because I'm ignorant and a newb when it comes to the innards of inheritance...
Quote:
 class Derived_foo1{Derived_foo1(const Derived_foo1& rhs):Base_foo1(rhs),Base_foo2(rhs){}}

Why does that work? 'rhs' is not a Base_foo1 or Base_foo2 object... its got more 'bits' too it... so how would the default copy ctors for Base_foo1 and Base_foo2 know how to deal with a Derived_foo object correctly? (Btw, I take it you meant to use Derived_foo in your example and not Derived_foo1 as you wrote?)

Share on other sites
Washu    7829
Quote:
Original post by Timkin
Quote:
 class Derived_foo1 : public Base_foo1, public Base_foo2{Derived_foo1(const Derived_foo1& rhs):Base_foo1(rhs),Base_foo2(rhs){}}

Why does that work? 'rhs' is not a Base_foo1 or Base_foo2 object... its got more 'bits' too it... so how would the default copy ctors for Base_foo1 and Base_foo2 know how to deal with a Derived_foo object correctly? (Btw, I take it you meant to use Derived_foo in your example and not Derived_foo1 as you wrote?)

Actually, it is a Base_foo1 and a Base_foo2 (since it derived from them), and that's exactly what their copy constructors will see too...now

Share on other sites
Timkin    864
Quote:
 Original post by WashuActually, it is a Base_foo1 and a Base_foo2 (since it derived from them), and that's exactly what their copy constructors will see too...now

So your saying that any base class will only ever see the bits of a derived class that are derived from it, even if you throw a derived class object at the base class method?

Share on other sites
SiCrane    11839
There's a reference conversion of a derived type to the base type in each copy constructor. The reference will be cast to the first base type then the second base type, and the relevant pointer arithmetic will automagically happen.

Share on other sites
Timkin    864
Quote:
 Original post by SiCraneThere's a reference conversion of a derived type to the base type in each copy constructor. The reference will be cast to the first base type then the second base type, and the relevant pointer arithmetic will automagically happen.

Thank you all for improving my understanding of how inheritance works. Knowing this last tidbit certainly helps to explain a few things... ;)