Effective C++ Item 12 Copy all parts of an object

时间:2014-03-01 00:08:54   收藏:0   阅读:484

This one is simple, do not forget to copy all parts of an object in copy constructor or assignment operator!

There are two cases you tend to make mistakes.

 

1. Make sure you modify copy constructor and assignment operator, if you add a member to a class.

2. Make sure you call the copy constructor and assignment opeator of base class in derived class.

 

A concrete example is that:

member variable "priority" belongs to PriorityCustomer not Customer

PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs):
	Customer(rhs), 	// call base class‘ copy constructor
	priority(rhs.priority) {
		...
	}
}

PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs) {
	...
	Customer::operator=(rhs);
	priority = rhs.priority;
	return *this;
}

  

Effective C++ Item 12 Copy all parts of an object,布布扣,bubuko.com

原文:http://www.cnblogs.com/xinsheng/p/3573085.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!