- 论坛徽章:
- 0
|
By default, class objects can be copied. In particular, a class object can be initialized with a copy of another object of the same class. This can be done even where constructors have been declared.
By default, the copy of a class object is a copy of each member. If that default is not the behavior wanted for a class X, a more appropriate behavior can be provided by defining a copy constructor, X::X(const X&).
By Bjarne Stroustrup
If the class definition does not explicitly declare a copy constructor and there is no user-declared move
constructor, a copy constructor is implicitly declared as defaulted (8.4). Thus, for the class definition
struct X { X(const X&, int);
};
a copy constructor is implicitly-declared. If the user-declared constructor is later defined as
X::X(const X& x, int i =0) { /∗ ... ∗/ }
then any use of X’s copy constructor is ill-formed because of the ambiguity; no diagnostic is required.
12.8 Copying and moving class objects, Programming Languages — C++, ISO/IEC JTC1 SC22 WG21 N 3092 |
|