Reputation: 1506
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
Upvotes: 1
Views: 65
Reputation: 254501
A default copy constructor or default copy assignment copies all elements of of the class.
"Default" here is an unconventional way of saying "implicitly defined".
Since the class Y
doesn't declare a copy constructor (i.e. a constructor Y(Y&)
or Y(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 of Y
(just a single member in this case), as if you'd written something like:
Y::Y(Y const &other) : a(other.a) {}
If this copy cannot be done, it is an error to try to copy an object of class.
However, this constructor can only be defined if all the members can be copied. In this case unique_handle
has a private copy constructor, which can't be called from this constructor.
Similarly, since Y
doesn't declare a copy-assignment operator (i.e. operator=(Y&)
or operator=(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:
Y & Y::operator=(Y const & other) {
a = other.a;
return *this;
}
which again would fail, since unique_handle
has a private copy-assignment operator.
Upvotes: 1
Reputation: 258618
class
members are private
by default, struct
members are public
by default.
Because you didn't specify an access modifier to the constructors in your class, they are private, and therefore inaccessible from the outside.
Upvotes: 0
Reputation: 76828
If your class contains an element that cannot be copied (unique_handle
has a private copy-constructor, so it can indeed not be copied), then an element-wise copy is not possible.
Upvotes: 2