You can, of course, implement both the copy constructor and the copy assignment operator in a derived class, but this is probably a sign of poor design. Take this example:
#include <iostream> class Base { public: Base() {} private: Base(const Base& other) { std::cout << "Base copy constructor invoked!" << std::endl; } }; class Derived : public Base { public: Derived() {} Derived(const Derived& other) { std::cout << "Derived copy constructor invoked!" << std::endl; } }; int main(int argc, char** argv) { Derived a; Derived b = a; return 0; }
It will be just fine. However, as expected, when you run the resulting program, all the printed value Derived copy constructor invoked! . When a base class declares its copy / copy constructor statement as private, this does not prevent derived classes from implementing their own versions. It simply prohibits derived classes from invoking base class versions .
And therein lies the problem: it is always good practice to make sure that you copy all parts of the object, so that you really have two different copies. Part of your object includes data that belongs to the base class, so you should always be sure that you invoke the constructor statement of the base class instance / copy instance to ensure a full copy. But this design data is not copied. Thus, it is not possible to copy all parts of an object.
This is for you if you want to stick to this design. One important thing to ask yourself is, does your derived class really need to be copied at all? If not, then there is nothing to worry about!
source share