If this is the object you are planning to copy, yes, it is a good idea. See my comment below if this is not the case.
If your virtual base class uses resources that must be explicitly allocated and copied (buffers, operating system objects, etc.), the definition of a copy constructor will save you from having to do this in each derived class separately (and, in addition, then you will not could do if these underlying resources were private using public inheritance).
eg:.
class Base { public: Base( const Base & ); virtual ~Base(); virtual void Method() = 0; // etc... private: int *memberIntArray; int length; // etc... }; class DerivedOne : public Base{ public: DerivedOne( const DerivedOne & ); virtual ~DerivedOne(); virtual void Method(); // etc... protected: // data, etc... }; class DerivedTwo : public Base{ public: DerivedTwo( const DerivedTwo & ); virtual ~DerivedTwo(); virtual void Method(); // etc... protected: // data, etc... }; /////////////////// Base::Base( const Base & other ) { this->length = other.length; this->memberIntArray = new int[length]; memcpy(this->memberIntArray,other.memberIntArray,length); } //etc... DerivedOne::DerivedOne( const DerivedOne & other ) : Base( other ) { //etc... } DerivedTwo::DerivedTwo( const DerivedTwo & other ) : Base( other ) { //etc... }
source share