条款12:复制对象时勿忘其每一个部分

设计良好的面向对象系统会将对象的内部封装起来,只留两个函数负责对象拷贝,即copy构造函数与copy assignment操作符。编译器会在必要的时候为类创建coping函数,并说明这些“编译器生成版”的行为:将被拷贝对象的所有成员变量都做一份拷贝。

任何时候,只要自己实现派生类的copying函数,则必须很小心的复制其基类成分。这些成分往往是private私有的,故无法直接访问它们,因此应该让派送类的coping函数调用相应的基类函数:

 void logCall(const string& funcName);
class Customer { public:
     ... Customer(const Customer& rhs); Customer& operator=(const Customer& rhs);
     ... private: string name; Date lastTranscation; };

  

 class PriorityCustomer : public Customer
 {
 public:
     ...
	 PriorityCustomer(const PriorityCustomer& rhs);
	 PriorityCustomer& operator=(const PriorityCustomer& rhs);
      ...
 private:
	 int priority;
 };

 PriorityCustomer ::PriorityCustomer (const PriorityCustomer& rhs) 
	 : Customer(rhs),	//调用基类的copy构造函数
	 priority(rhs.priority)
 {
	 logCall("PriorityCustomer copy constructor");
 }

 PriorityCustomer& PriorityCustomer ::operator = (const PriorityCustomer& rhs) 
 {
	 logCall("PriorityCustomer copy assignment constructor");
	 Customer::operator=(rhs);	//对基类Customer成分进行复制动作
	 priority = rhs.priority;
	 return *this;
 }

当编写一个copying函数,确保1、复制所有local成员变量,2、调用所有基类的适当的copying函数。

注意两个错误用法:1、令copy assignment操作符调用copy构造函数是错误的,因为在这就像试图构造一个已存在的对象。

         2、令copy构造函数调用copy assignment操作符同样是错误的。构造函数用来出事后对象,而assignment操作符只实行与已初始化的对象身上。对一个尚未构造好的对象赋值,就像在一个尚未初始化的对象身上做“z只对已初始化对象才有意义”的事意义。

消除copy构造函数与copy assignment操作符重复代码的做法是:建立一个新的成员函数给两者调用。这样的函数往往是private而且被命名为init。这个策略可以安全消除copy构造函数与copy assignment操作符之间的代码重复。

请牢记:

  1、copying 函数应该确保复制“对象内的所有成员变量”及“所有基类成分”。

  2、不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。

原文地址:https://www.cnblogs.com/lwenwen/p/3468569.html