Advertisement

Deep copy of a LL of virtual base classes

Started by June 02, 2001 09:42 AM
4 comments, last by Succinct 23 years, 8 months ago
Anybody know how to perform a deep copy of a linked list of virtual base classes? The problem here, is that the exact constructor to be called is unknown... Is it possible? Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Succinct Demos Online~
-- Succinct(Don't listen to me)
Use the virtual constructor idiom.

Your base class specify a virtual Create() method that returns an instance of the class. Each dervied class overrides it.

So insteading of calling new, you call the Create() method to allocate the object.

Similar to the prototype pattern in design patterns, I think.

..
Advertisement
Ahhh, now that sounds like a plan, void!

thanks lots!
-- Succinct(Don't listen to me)
I suppose it is just a matter of style, but it seems that CreateCopy or CreateNew would be a better choice in names. I would expect Create to do what would normally be done in the constructor and Destroy to do what would have been done in the destructor. There are sometimes benefits to seperating the physical and logical allocation/deallocation. One example is reducing memory fragmentation.
Keys to success: Ability, ambition and opportunity.
I agree, LilBudyWizer.

CreateCopy could take a parameter, the object being copied, too.

Neat stuff.

Thanks, guys.

-- Succinct
-- Succinct(Don't listen to me)
I believe the "industry"(c++ moderated newsgroup) names the function Clone()

This topic is closed to new replies.

Advertisement