If you have an empty class, compilers will declare a default constructor, a copy constructor, a copy assignment operator and a destructor for you. So
1 | class Empty{}; |
will be essentially the same as
1 | class Empty |
Let’s see an example of how compiler will refuse to generate some code for you and you should write your own code explicitly.
1 |
|
will have such compilation errors:
1 | test.cpp: In member function ‘Pair& Pair::operator=(const Pair&)’: |
It means that the compiler refuse to generate copy assignment constructor for line 20 when we are going to assign b
to a
. Why? The simple answer is that compilers don’t know what to do in copy constructor. Since C++ doesn’t provide a way to make a reference refer to a different object, neither can we modify a const object.
So the solution is if you want to support copy assignment in a class containing reference members or const members, you must define the copy assignment operator yourself. Finally, compilers reject implicit copy assignment operators in derived classes that inherit from base classes declaring the copy assignment operator private, in other words, if base classes prevent doing something such as copy assignment, derived classes inherit that property from base classes implicitly (unless you explicitly define copy assignment operator in derived classes). After all, compiler-generated copy assignment operators for derived classes are supposed to handle base class parts too, but in doing so, they certainly can’t invoke member functions the derived class has no right to call.