There are seven special functions automatically generated by compiler. The following list summarize these functions.
class X {
public:
X(Sometype); // ‘‘ordinary constructor’’: create an object
X(); //default constructor
X(const X&); // copy constructor
X(X&&); //move constructor
X& operator=(const X&); // copy assignment: clean up target and copy
X& operator=(X&&); // move assignment: clean up target and move
˜X(); //destructor: clean up
// ...
};
Except for the ‘‘ordinary constructor,’’ these special member functions will be generated by the compiler as needed. If you want to be explicit about generating default implementations, you can:
class Y {
public:
Y(Sometype);
Y(const Y&) = default; // I really do want the default copy constructor
Y(Y&&) = default; // and the default move constructor
// ...
};
If you are explicit about some defaults, other default definitions will not be generated.
To complement =default, we have =delete to indicate that an operation is not to be generated. A
base class in a class hierarchy is the classical example where we don’t want to allow a memberwise Copy.
class Shape {
public:
Shape(const Shape&) =delete; // no copy operations
Shape& operator=(const Shape&) =delete;
// ...
};
Comments