如果您有一个具有很多属性的类,则默认值对于复制构造函数更有用。例如,如果你有这个类:
class MyClass {
private:
int offset;
std::string name;
std::vector<Person*> relatives;
float weight;
MyClass* spouse;
Vehicle* car;
double houseArea;
Date birth;
Person* parents[2];
public:
/* Default constructor will be defined here */
};
而不是以这种方式定义复制构造函数:
MyClass(const MyClass& that) :
offset(that.offset),
name(that.name),
relatives(that.relatives),
weight(that.weight),
spouse(that.spouse),
car(that.car),
houseArea(that.houseArea),
birth(that.birth),
parents(that.parents)
{}
你会这样定义:
MyClass(const MyClass&) = default;