Appending = default to a special member function tells the compiler to generate the default version, which works on each field in declaration order:
class Circle {
public:
// Default-initialise each field. Built-in types are left uninitialised unless given a default value.
Circle() = default;
// Copy-construct each field from the other object. Built-in types (including pointers) are copied.
Circle(const Circle&) = default;
// Move-construct each field from the other object. Built-in types (including pointers) are copied.
Circle(Circle&&) = default;
// Copy-assign each field from the other object.
Circle& operator=(const Circle&) = default;
// Move-assign each field from the other object.
Circle& operator=(Circle&&) = default;
// Destroy each field in reverse order. Raw pointers are not deleted.
~Circle() = default;
};Suppressed special member functions can be restored:
class Circle {
public:
Circle(double r);
Circle() = default; // Suppressed by the constructor above.
double radius;
};Special member functions can change their accessibility:
class Circle {
public:
Circle(double r);
double radius;
protected:
Circle(const Circle&) = default;
Circle& operator=(const Circle&) = default;
};Special member functions can be explicitly virtual:
class Circle {
public:
virtual ~Circle() = default;
virtual Circle& operator=(const Circle&) = default;
};Comparison operators can be defaulted to compare every field in declaration order:
class Circle {
public:
bool operator==(const Circle&) const = default;
bool operator<(const Circle&) const = default;
bool operator<=(const Circle&) const = default;
bool operator>(const Circle&) const = default;
bool operator>=(const Circle&) const = default;
auto operator<=>(const Circle&) const = default;
double radius; // Compares radius first.
double stroke; // Then compares stroke.
};
Dan