C++复制对象时勿忘每一部分

确保两点: 1 复制所有的成员变量 2 调用所有基类的copy函数

class Customer
{
public:
	explicit Customer(const tstring _name,const tstring _lastTransaction)
		:m_sName(_name), m_sLastTransaction(_lastTransaction)
	{
		OutputDebugString(_T("Customer construct \n"));
	}
	virtual ~Customer(){
		OutputDebugString(_T("Customer deconstruct \n"));
	}

	Customer(const Customer& customer) : m_sName(customer.m_sName)
	{
		OutputDebugString(_T("Customer copy construct \n"));
	}
	Customer& operator=(const Customer& customer)
	{
		m_sName = customer.m_sName;
		OutputDebugString(_T("Customer operater \n"));
		return *this;
	}
private:
	tstring m_sName;
	tstring m_sLastTransaction;
};

class  PriorityCustomer: public Customer
{
public:
	explicit PriorityCustomer(const tstring name,const tstring lastTime,int nPriority):
		m_nPriority(nPriority), Customer(name, lastTime)
	{
		OutputDebugString(_T("PriorityCustomer construct \n"));
	}

	PriorityCustomer(const PriorityCustomer& rhs) :m_nPriority(rhs.m_nPriority),
		Customer(rhs)
	{
		OutputDebugString(_T("PriorityCustomer copy construct\n"));
	}

	PriorityCustomer& operator=(const PriorityCustomer rhs)
	{
		OutputDebugString(_T("PriorityCustomer operater \n"));
		m_nPriority = rhs.m_nPriority;
		Customer::operator=(rhs);
		return *this;
	}
	virtual ~PriorityCustomer(){
		OutputDebugString(_T("PriorityCustomer deconstruct \n"));
	}
private:
	int m_nPriority;
};

时间: 2024-08-24 21:45:14

C++复制对象时勿忘每一部分的相关文章

EC笔记:第二部分:12、复制对象时勿忘其每一个成分

EC笔记:第二部分:12.复制对象时勿忘其每一个成分 1.场景 某些时候,我们不想使用编译器提供的默认拷贝函数(包括拷贝构造函数和赋值运算符),考虑以下类定义: 代码1: class Point{ private: double x; double y; public:         Point()=default; Point(const Point &other){ x=other.x; y=other.y; } const Point& operator=(const Point

Effective C++_笔记_条款12_复制对象时勿忘其每一个成分

(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 编译器会在必要时候为我们的classes创建copying函数,这些“编译器生成版”的行为:将被烤对象的所有成员变量都做一份拷贝. 如果你声明自己的copying函数,意思就是告诉编译器你并不喜欢缺省实现中的某些行为.编译器仿佛被冒犯似的,会以一种奇怪的方式回敬:当你的实现代码几乎必然出错时却不告诉你.所以自己实现copying函数时,请遵循一条规则:如果你为c

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

(一) 一个继承体系的声明: class Date {...}; class Customer { public: ... private: string name; Date lastTransaction; }; class PriorityCustomer : public Customer { public: PriorityCustomer(const PriorityCustomer& rhs); PriorityCustomer& operator=(const Priori

Effective C++读书笔记之十二:复制对象时勿忘其每一个成分

Item 12:Copy all parts of an object 如果你声明自己的copying函数,意思就是告诉编译器你并不喜欢缺省显示中的某些行为.而编译器会对"你自己写出copying函数"做出一种复仇的行为:既然你拒绝它们为你写出copying函数,如果你的代码不完全,它们也不会告诉你.结论很明显:如果你为class添加一个成员变量,你必须同时修改copying函数.如果你忘记,编译器不太可能提醒你. 一下提供一种正确的模版: class Date{...}; class

[012]复制对象时勿忘其每一个成分

引言: 在深拷贝和浅拷贝的理解中,我们知道了“拷贝构造函数”一词,并且也了解了它的构成. A(const A& r); // 形式有多种,在这里只列出一个 因此,在值传递的应用场景里,我们可以写出以下的拷贝构造函数: 1 #include <iostream> 2 #include<string> 3 using namespace std; 4 5 class A { 6 public: 7 A(int i) : count(i) {}; 8 A(const A&

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

对象复制操作operator=或copy构造函数,一定要记得复制对象每一个成份,特别是base class的成分: 注意:

Effective C++ -----条款12: 复制对象时勿忘其每一个成分

Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个coping函数共同调用.如果你发现你的copy构造函数和copy assignment操作符有相近的代码,消除重复代码的做法是,建立一个新的成员函数给两者调用.这样的函数往往是private而且常被命名为init.这个策略可以安全消除copy构造函数和copy assignment操作符之间的代码重复.

effective c++ 条款12:复制对象时勿忘其每一个成分

记住:拷贝函数应该确保复制"对象内的所有成员变量"及"所有父类成分".不要尝试以某个拷贝函数实现另一个拷贝函数.应该将共同机能放进第三个函数中,并由两个拷贝函数共同调用. 下面是一个类实现了自己的拷贝函数,一起正常. void logCall(const string& funcName): class Customer { public: ... Customer(const Customer& rhs); Customer& operat

条款12:复制对象时请勿忘每一个成分

条款12:复制对象时请勿忘每一个成分 当为一个类实现自己的构造函数,相关赋值函数,析构函数,则必须有责任对类中的每一个成员进行初始化.赋值.释放.因此:如果为一个类添加一个成员,就必须同时相应修改上面几类函数. 看一个简单的类 class Terminal { Terminal(const int termid) : m_termId(termid) {} ~Terminal() {} Terminal(const Terminal & terminal) { this->m_termId