首页 > 编程语言 > 详细

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

时间:2014-03-01 00:08:54      阅读:484      评论:0      收藏:0      [点我收藏+]

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!