Sign in to follow this  

overriding derived class copy constructor

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Huh? 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by Washu
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


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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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.


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

Share this post


Link to post
Share on other sites

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this