Item 12-复制对象时忽忘其每一个成分(Copy all parts of an object)
设计良好之面向对象系统(OO-system)会将对象的内部封装起来,只留两个函数负责将对象拷贝(复制),那便是带着适切名称的copy构造函数和copy assignment操作符,称它们为copying函数。
如果是"编译器生成版本"的行为:将被拷对象的所有成员变量都做一份拷贝。
如果是自己声明就要注意了。
如果是为"derived class撰写copying函数"的重大责任,必须很小心的复制其base class成分。那些成分往往是private,所以你无法直接访问它们,你应该让derived class的copying函数调用相应的base class函数。
Ex:
class Customer //base class { public: ... Customer(const Customer& rhs); Customer& operator = (const Customer& rhs); ... private: string name; } Customer::Customer(const Customer& rhs):name(rhs.name); Customer::Customer& operator=(const Customer& rhs) { name=rhs.name; return *this; } class PriorityCustomer:public Customer //derived class { public: ... PriorityCustomer(const PriorityCustomer& rhs); PriorityCustomer& operator=(const PriorityCustomer& rhs); ... private: int priority; } PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs):Customer(rhs),priority(rhs.priority); PriorityCustomer::operator=(const PriorityCustomer& rhs) { Customer::operator=(rhs) //对base class成分进行赋值动作 priority=rhs.priority; return *this; }
"复制每一个成分"即复制所有local成员变量,调用所有base classes内的适当的copying函数
令copy assignment操作符调用copy构造函数是不合理的,因为这就像试图构造一个已经存在的对象。
令copy构造函数调用copy assignment操作符也没有意义。
如果copy构造函数和copy assignment操作符有相近的代码,消除重复代码的做法是,建立一个新的成员函数给两者调用。这样的做法往往private,而且常被命名为init。这个策略可以安全消除copy构造函数和copy assignment操作符之间的代码重复。
请记住:
copying函数应该确保复制"对象内的所有成员变量"及所有"base class"成分
不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。
时间: 2024-12-20 10:05:35