Copy all parts of an objec
设计良好之面向对象系统(OO-systems)会将对象的内部封装起来,只留两个函数负责对象拷贝,那便是带着适切名称的copy构造函数和copy assignment操作符,我称他们为copying函数。编译器会在必要时为我们的classes创建copying函数,并说明这些“编译器生成版”的行为:将被拷对象的所有成员变量都做一份拷贝。
如果你声明自己的copying函数,意思就是告诉编译器你不喜欢缺省实现的某些行为。编译器仿佛被冒犯似的会以一种奇怪的方式回敬:当你的实现代码几乎必然出错时却不告诉你。
考虑一个class用来变现顾客,其中手工写出copying函数,使得外界对它们的调用会被记下来:
void logCall(const std::string & funcName);
class Customer{
public:
Customer(const Customer& rhs);
Customer& operator=(const Customer& rhs);
private:
std::string name;
};
Customer::Customer(const Customer& rhs)
: name(rhs.name)
{
logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs)
{
logCall("Customer copy assignment operator");
name = rhs.name;
return *this;
}
这里的每一件事情看起来都很好,而实际上每件事也的确都好,直到另一个成员变量加入战局:
class Date {...};
class Customer{
public:
Customer(const Customer& rhs);
Customer& operator=(const Customer& rhs);
private:
std::string name;
Date lastTransaction;
};
这时候既有的copying函数执行的是局部拷贝:它们的确复制了顾客的name,但没有赋值新添加的lastTransaction。大多数编译器对此不出任何怨言-----即使在最高警告级别中。结论很明显:如果你为class添加一个成员变量,你必须同时修改copying函数。一旦发生继承,可能会造成此一主题最暗中肆虐的一个潜藏危机。试考虑:
class PriorityCustomer: public Customer{
public:
PriorityCustomer(const PriorityCustomer& rhs);
PriorityCustomer& operator=(const PriorityCustomer& rhs);
private:
int priority;
};
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: priority(rhs.priority)
{
logCall("PriorityCustomer copy constructor");
}
PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment operator");
priority = rhs.priority;
return *this;
}
PriorityCustomer的copying函数看起来好像复制了PriorityCustomer内的每一样东西,但实际上它还内含了Customer成员变量复件,而那些成员变量却未被复制。PriorityCustomer的copy构造函数并没有指定实参传给其base class构造函数,因此PriorityCustomer对象的Customer成分会被不带实参的Customer构造函数(即default构造函数----必定有一个否则无法编译)初始化。default构造函数将针对name和lastTransaction执行缺省的初始化动作。
以上事态在PriorityCustomer的copy assignment操作符身上只有轻微不同。它不曾企图修改其base class的成员变量,所以那些成员变量保持不变。
任何时候只要你承担起“为derived class撰写copying函数”的重任,必须很小心的也复制其base class成分。那些成分往往是private,所以你无法直接访问它们,你应该让derived class的copying函数调用相应的base class函数:
class PriorityCustomer: public Customer{
public:
PriorityCustomer(const PriorityCustomer& rhs);
PriorityCustomer& operator=(const PriorityCustomer& rhs);
private:
int priority;
};
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: Customer(rhs), priority(rhs.priority)
{
logCall("PriorityCustomer copy constructor");
}
PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment operator");
Customer::operator=(rhs);
priority = rhs.priority;
return *this;
}
本条款题目所说的“复制每一个成分”现在应该很清楚了。当你编写一个copying函数,请确保(1)赋值所有local成员变量,(2)调用所有base classes内的适当的copying函数。这两个copying函数往往有近似相同的实现本体,这可能会诱使你让某个函数调用另一个copying函数却无法让你达到你想要的目标。令copy assignment操作符调用copy构造函数是不合理的,因为这就像试图构造一个已经存在的对象。反方向,令copy构造函数调用copy assignment操作符,同样无意义。构造函数用来初始化对象,而assignment操作符只施行于已初始化对象身上。对一个尚未构造好的对象赋值,就像在一个尚未初始化的对象身上做“只对已初始化对象才有意义”的事一样。
请记住:
- Copying函数应该确保复制“对象内的所有成员变量”及”所有base class成分“。
- 不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。