I want to copy objects in c++. The problem is that i have derived classes with polymorphism, as shown i the pseudocode below:
class BaseCl { some virtual functions };
class DerivedClass : public BaseCl { ... };
...
BaseCl * b1 = new DerivedClass();
BaseCl * b2 = new "copy of b1"; (just pseudocode)
The problem is the last line:
I want to copy an object of the class "BaseCl", but because of the polymorphism the copy must be just like the original object of "DerivedClass".
What is the best way to do that?
Thank you very much, any help is appreciated.
Edit: Problem has been solved:
Inserted:
virtual BaseCl *clone() = 0;
in the base class and
DerivedCl *clone() {return new DerivedCl(*this);}
in the derived class. Thank you all.