deep copy

Started by
2 comments, last by TheJakub 20 years, 9 months ago
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?
Advertisement
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.
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.
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.

This topic is closed to new replies.

Advertisement