12. Copy all parts of an object

复制对象时勿忘其每一部分

将copy构造函数和copy assignment函数统称为copying函数。
如果自己声明了copying函数,编译器将不会再次声明。

考虑以下代码:

void LogCall(const std::stirng& funcName);

class Customer 
{
public:
	...
	Customer(const Customer& rhs);
	Customer& operator = (const Customer& rhs);
	...
private:
	std::string name;
};

Cumstomer::Customer(const Customer& rhs)
	: name(rhs.name)
{
	LogCall("Customer copy constructor"); 
}

Customer& Customer::operator = (const Cumstomer& rhs)
{
	Logcall("Customer copy constructor");
	name = rhs.name;
	return *this;
}

在添加数据成员时,可能会忘记在Copying函数中添加相应的处理。

例如此时再在Customer添加一个成员std::string addr,
你就必须在copying函数中添加对addr的赋值操作。
如果忘记添加,编译器不会有任何提示,这就将成为一个隐藏的bug。

类继承中,也会经常遗忘对基类相关函数的调用。

class PriorityCustomer : public Cumstomer
{
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;
}

上面的copying函数,都遗忘了对base class部分的处理。
修改后的代码如下:

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函数。应该将共同技能放进第三个函数中,并由两个函数共同调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值