Jump to content
  • Advertisement
Sign in to follow this  
Timkin

overriding derived class copy constructor

This topic is 4701 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!