This has been bugging me lately. Say I have a base class Base. If I have multiple derived classes on top of Base, such as DerivedA and DerivedB, a deep copy gets to be a pain.
OtherClass(const OtherClass & _rhs)
{
//I have a list of Base *, now I must assign a class id to each derived class to properly create a new one.
//...
}
Is there any way to get around this?
You should define a clone method in your Base class:
Each derived class implement that clone method:
Then your OtherClass just has to iterate and call clone method over each instance of
Base*in your list.