What does this statement mean?
A default copy constructor or default copy assignment copies all
elements of of the class. If this copy cannot be done, it is an error
to try to copy an object of class.
For example:
class unique_handle{
unique_handle(const unique_handle&);
unique_handle&operator=(const unique_handle&);
public ://...
};
struct Y {
unique_handle a;
}//require explicit initialization
Y y1;
Y y2=y1; //error:cannot copy Y::a
“Default” here is an unconventional way of saying “implicitly defined”.
Since the class
Ydoesn’t declare a copy constructor (i.e. a constructorY(Y&)orY(Y const &)), then one is implicitly declared. When you try to use it, it is implicitly defined to copy all the base-class objects and members ofY(just a single member in this case), as if you’d written something like:However, this constructor can only be defined if all the members can be copied. In this case
unique_handlehas a private copy constructor, which can’t be called from this constructor.Similarly, since
Ydoesn’t declare a copy-assignment operator (i.e.operator=(Y&)oroperator=(Y const&)), then one is again implicitly declared. If you tried to use it, then it would be implicilty defined to assign all the members, as if you’d written:which again would fail, since
unique_handlehas a private copy-assignment operator.