Effective C++ Item12:复制对象时勿忘其每一个成分

自动生成的拷贝构造函数会帮我们将对象的所有部分都进行拷贝,如果我们自己声明拷贝构造函数:

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

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

Customer& Customer::operator=(const Custom& rhs)
{
	logCall("Customer copy assignment operator");
	name = rhs.name;
	return *this;
}

手动生成拷贝构造函数,希望外界对他们的调用会被log记录下来。
这里一切看起来都很好,直到我们加入另外一个成员变量:

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

这时如果我们不更新上面的两个函数的话, 新加入的lastTransaction 就没有被复制,结论就是,如果我们自己生成了拷贝函数,在添加新的成员变量之后,拷贝函数也需要相应的被修改。
一旦发成了继承,会导致一个危机:

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的拷贝构造函数复制了声明的所有成员变量,但是他从父类继承的成员变量却没有被复制,所以你还需要显式的调用父类的拷贝构造函数:

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

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: Customer(rhs)
, priority(rhs.priority) 
{
	logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
	logCall("PriorityCustomer copy assignment operator");
	Curtomer::operator=(rhs);
	priority = rhs.priority;
	return *this;
}

所以每当我们自己编写拷贝函数,需要做到

  1. 复制所有的local变量
  2. 调用base class的拷贝函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值