您的位置:首页 > 移动开发 > Objective-C

Item12: Copy all parts of an object

2010-05-14 08:39 351 查看
设计良好的面向对象系统(OO-systems)会将对象的内部封装起来,只留两个函数负责对象拷贝(复制),那就是copy构造函数和copy assignment操作符,我们称之为copying函数。

如果你声明自己的copying函数,意思就是告诉编译器你不喜欢缺省实现中的某些行为。编译器仿佛被冒犯似的,会以一种奇怪的方式回敬:当你的实现代码几乎必然出错时却不告诉你。

考虑一个class用来表现顾客,copying函数是手工写出的,使得外界对它们的调用会被志记(logged):

void logCall(const std::string& funcName);          // make a log entry

class Customer {

public:

...

Customer(const Customer& rhs);

Customer& operator=(const Customer& rhs);

...

private:

std::string name;

};

Customer::Customer(const Customer& rhs)

: name(rhs.name)                                 // copy rhs's data

{

logCall("Customer copy constructor");

}

Customer& Customer::operator=(const Customer& rhs)

{

logCall("Customer copy assignment operator");

name = rhs.name;                               // copy rhs's data

return *this;                                  // see Item 10

}


到目前为止一切都看起来很好。直到另一个成员变量加入:

class Date { ... };       // for dates in time

class Customer {

public:

...                     // as before

private:

std::string name;

Date lastTransaction;

};


这时候 既有的copying函数执行的是局部拷贝(partial copy):它们的确复制了顾客的name,但没有复制新添加的lastTransaction。大多数编译器对此不报错——即使在最高警告级别中(见条款53)。这是编译器对“你自己写出copying函数”的复仇行为:既然你拒绝它们为你写出copying函数,如果你的代码不完全,它们也不告诉你。

一旦发生继承,可能会造成一个潜在的危机。试考虑:

class PriorityCustomer: public Customer {                  // a derived class

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内的每一样东西,但请看一眼。是的,它们复制了PriorityCustomer声明的成员变量,但每个PriorityCustomer还内含它所继承的Customer成员变量副本,而那些成员变量却未被复制。PriorityCustomer的copy构造函数并没有指定实参传给其base class构造函数,因此PriorityCustomer对象的Customer成分会被不带实参的Customer构造函数(即default构造函数——必定有一个否则无法通过编译)初始化。default构造函数将针对name和lastTransaction执行缺省的初始化动作。

任何时候只要你承担起“为derived class撰写copying函数”的重大责任,必须很小心地复制其base class成分。那些成分往往是private。

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)

:    Customer(rhs),                   // invoke base class copy ctor

priority(rhs.priority)

{

logCall("PriorityCustomer copy constructor");

}

PriorityCustomer&

PriorityCustomer::operator=(const PriorityCustomer& rhs)

{

logCall("PriorityCustomer copy assignment operator");

Customer::operator=(rhs);           // assign base class parts

priority = rhs.priority;

return *this;

}


本条款是上所说的“复制每一个成分”现在应该很清楚了。当你编写一个copying函数,请确保(1)复制所有local成员变量,(2)调用所有base classes内的适当的copying函数。

这两个copying函数往往有近似相同的实现,这可能会诱惑你让某个函数调用另一个函数以避免代码重复。想法是好的,请不要这样做。

令copying assignment操作符调用copy构造函数是不合理的,因为这就像试图构造一个已经存在的对象。同时,令copy构造函数调用copy assignment操作符——同样无意义。构造函数用来初始化新对象,而assignment操作符只施行于已初始化对象身上。

如果你发现copy构造函数和copy assignment操作符有相近的代码,消除重复代码的做法是,建立一个新的成员函数给两者调用。这样的函数往往是private而且常被命名为init。

请记住:

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

2、不要尝试以某个copying函数实现另一个copying函数。应该将共同功能放进第三个函数中,并由两个copying函数共同调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: