Archived

This topic is now archived and is closed to further replies.

TheJakub

deep copy

Recommended Posts

TheJakub    122
Say I have 2 classes: class One{ public: One(); One(const One& obj); virtual ~One(); ... }; class Two: Public One{ public: Two() : One(){} Two(const Two& obj); ~Two(); ... Two* Clone(){ return new Two(*this); } ... }; What I am looking to do is a deep copy. When I declare a new "Two" object, a base "One" constructor is called. Is there a way I can call the base "One" copy constructor when I call Two's Clone() method?

Share this post


Link to post
Share on other sites
HellRaider    122
Yes.
Curiously, you were able to write this:
Two() : One() {}

But didn''t try this:
Two(const Two &obj) : One(obj) {}

Which would do what you want.

Share this post


Link to post
Share on other sites
TheJakub    122
lol I knew I was close. Thanks for the help. For some reason my mind kept telling me you cant do that because its a Two class, but my mind didnt go the direction of reminding me that Two is inherited. geez..

Thanks again.

Share this post


Link to post
Share on other sites
Miserable    606
By the way, there''s no need to explicitly call the base class default constructor in a child class, as this is done automatically (objects are created from base to ultimate descendant, so ''ancestor'' classes'' constructors are called automatically). You only need to call the parent constructor explicitly if you need to call a non-default constructor.

Share this post


Link to post
Share on other sites