复制对象时勿忘其每一个成分——条款12

        设计良好之面向对象系统(OO-systems)会将对象的内部封装起来,只留两个函数负责对象拷贝(复制),那便是带着适切名称的copy构造函数和copy assignment操作符,我称它们为copying函数。条款5观察到编译器会在必要的时候为我们的classes创建copying函数,并说明这些“编译器生成版”的行为:将被拷对象的所有成员变量都做一份拷贝。

        如果你声明自己的copying函数,意思就是告诉编译器你并不喜欢缺省实现中的某些行为。编译器则不会告诉你copying时犯的错误。

void logCall(const std::string& funcName);  // 制造一个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)                             // 复制rhs的数据
{
	logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs)
{
	logCall("Customer copy assignment operator");
	name = rhs.name;                                 // 复制rhs的数据
	return *this;                                    // 见条款10
}

         这里的每一件事情看起来都很好,而实际上每件事情也的确都好,直到另一个成员变量加入战局:

class Date {...};
class Customer {
public:
	...
private:
	std::string name;
	Date lastTransaction;
}

       这时候既有的copying函数执行的是局部拷贝:它们的确复制了顾客的name,但没有复制新添加的lastTransaction。大多数编译器对此不出任何怨言——即使在最高警告级别中(见条款53)。这是编译器对“你自己写出copying函数”的复仇行为。

具体实例:

#include<iostream>
using namespace std;

void logCall(const std::string& funcName)  // 制造一个log entry
{
	cout<<funcName<<endl;
}
class Customer {
public:
	Customer(string name, string address);
	Customer(const Customer& rhs);
	Customer& operator=(const Customer& rhs);
	void printCustomer();
private:
	std::string name;
	std::string address;
};

Customer::Customer(string name, string address)
	:name(name)
	,address(address)
{
	logCall("Customer constructor");
}

Customer::Customer(const Customer& rhs)
	:name(rhs.name)                             // 复制rhs的数据
{
	logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs)
{
	logCall("Customer copy assignment operator");
	name = rhs.name;                                 // 复制rhs的数据
	return *this;                                    // 见条款10
}

void Customer::printCustomer()
{
	cout<<"name:"<<name<<",address:"<<address<<endl;
}

int main(int argc, char* argv[])
{
	Customer cus("jack", "beijing");
	cus.printCustomer();
	Customer cus2(cus);
	cus2.printCustomer();
	return 0;
}

 输出:

Customer constructor
name:jack,address:beijing
Customer copy constructor
name:jack,address:

一旦发生继承,可能会造成此一主题更大的危机。试考虑:

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

        以上事态在PriorityCustomer的copy assignment操作符身上只有轻微不同。它不曾企图修改其base class的成员变量,所以那些成员变量保持不变。

        任何时候只要你承担起“为derived class撰写copying函数”的重责大任,必须很小心地复制其base class成分。那些成分往往是private(见条款22),所以你无法直接访问它们,你应该让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)    // 调用base class的copy构造函数
    ,priority(rhs.priority)
{
	logCall("PriorityCustomer copy constructor");
}

PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
	logCall("PriorityCustomer copy assignment operator");
	Customer::operator=(rhs);      // 对base class成分进行赋值动作
	priority = rhs.priority;
	return *this;
}

    本条款题目所说的“复制每一个成分”现在应该很清楚了。当你编写一个copying函数,请确保:

  1. 复制所有local成员变量;

  2. 调用所有base classes内的适当的copying函数。

请记住

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值