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

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie


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

示例:没对base class成分赋值

<pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;

class Customer{
public:
	Customer(){
	}
	Customer(string n):name(n){}
	Customer(const Customer &rhs) : name(rhs.name){
	}
	Customer &operator=(const Customer &rhs){
		name = rhs.name;
		return *this;
	}
	string getCustomer() const{return name;}
private:
	std::string name;
};

class PriorityCustomer: public Customer{
public:
	PriorityCustomer(int p, string n):Customer(n), priority(p){}
	PriorityCustomer(const PriorityCustomer &rhs): priority(rhs.priority){//derived class为调用base class的默认构造函数初始化
	}
	PriorityCustomer &operator=(const PriorityCustomer &rhs){
		//没对base class成分进行赋值
		priority = rhs.priority;
		return *this;
	}
	int getPriority() const	{return priority;}
	string getCustermerName() const{return getCustermerName();}
private:
	int priority;
};

int main(){
	PriorityCustomer pc1(1, "PriorityCustomer 1");
	PriorityCustomer pc2(pc1);
	PriorityCustomer pc3;
	pc3 = pc1;
	cout << pc2.getPriority() << endl
		<< pc3.getPriority() << endl;
	cout << pc2.getCustomerName() << endl
		<< pc3.getCustomerName() << endl;
}

 

输出:

1

1

(空)

(空)

解析:

PriorityCustomer的copy构造函数和copyassignment函数没有指定参数传给它的base class构造函数,因此PriorityCustomer对象的Customer成分会被Customer的默认构造函数初始化,所以它的name成员变量为被初始化为空字符串

纠正:对base class成分赋值

#include <iostream>
#include <string>
using namespace std;

class Customer{
public:
	Customer(){
	}
	Customer(string n):name(n){}
	Customer(const Customer &rhs) : name(rhs.name){
	}
	Customer &operator=(const Customer &rhs){
		name = rhs.name;
		return *this;
	}
	string getName() const{return name;}
private:
	std::string name;
};

class PriorityCustomer: public Customer{
public:
	PriorityCustomer(){}
	PriorityCustomer(int p, string n):Customer(n), priority(p){}
	PriorityCustomer(const PriorityCustomer &rhs): Customer(rhs),priority(rhs.priority){//调用base class的copy构造函数
	}
	PriorityCustomer &operator=(const PriorityCustomer &rhs){
		Customer::operator=(rhs); //对base class成分进行赋值动作
		priority = rhs.priority;
		return *this;
	}
	int getPriority() const	{return priority;}
	string getCustomerName() const{return getName();}
private:
	int priority;
};

int main(){
	PriorityCustomer pc1(1, "PriorityCustomer 1");
	PriorityCustomer pc2(pc1);
	PriorityCustomer pc3;
	pc3 = pc1;
	cout << pc2.getPriority() << endl
		<< pc3.getPriority() << endl;
	cout << pc2.getCustomerName() << endl
		<< pc3.getCustomerName() << endl;

	system("pause");
}
输出:
1
1
PriorityCustomer 1
PriorityCustomer 1

解析:

当你编写一个copying函数(包括copyconstructor和copy assignment)时,请确保(1)复制所有local成员变量,(2)调用所有base classes内的适当的copying函数


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值