EC读书笔记系列之7:条款12 复制对象时勿忘其每一个成分
记住:
★copying函数应确保复制“对象内的所有成员变量”及“所有base class成分”
★不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用
---------------------------------------------------------------
copying函数包括:copy constructor和copy assignment operator
引出问题的例子:
class Customer {
public:
...
Customer( const Customer &rhs );
Customer& operator=( const Customer &rhs );
...
private:
std::string name;
Date 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的copying函数看起来好像赋值了PriorityCustomer内的每个东西,但实际每个PriorityCustomer还内含有它所继承的基类:Customer成员变量复件,而那些成员变量却未被复制,这种情况特别容易忽视!!!
改进如下:
PriorityCustomer::PriorityCustomer( const PriorityCustomer &rhs )
:Customer( rhs ), //不要忘了调用base class的copy constructor!!!
priority( rhs.priority ) { logCall( "PriorityCustomer copy constructor" );
} PriorityCustomer& PriorityCustomer::operator=( const PriorityCustomer &rhs ) { logCall( "PriorityCustomer copy assignment operator" );
Customer::operator=( rhs ); //不要忘了对base class成分进行赋值操作
priority = rhs.priority;
return *this;
}
本条款所说的“复制每一个成分”,意指当你编写一个copying函数,应确保:
(1)复制所有local成员变量;
(2)调用所有base classes内的适当的copying函数
若发现copy constructor和copy assignment operator有相近的代码,为了消除重复代码,应建立一个新的成员函数给两者调用(此函数往往是private且常被命名为init)。此策略可以安全消除copy constructor和copy assignment operator之间的代码重复。
EC读书笔记系列之7:条款12 复制对象时勿忘其每一个成分的更多相关文章
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...
- Effective C++_笔记_条款12_复制对象时勿忘其每一个成分
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 编译器会在必要时候为我们的classes创建copying函数, ...
- EC笔记:第二部分:12、复制对象时勿忘其每一个成分
EC笔记:第二部分:12.复制对象时勿忘其每一个成分 1.场景 某些时候,我们不想使用编译器提供的默认拷贝函数(包括拷贝构造函数和赋值运算符),考虑以下类定义: 代码1: class Point{ p ...
- Effective C++ 条款12:复制对象时勿忘其每一个成分
void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...
- 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)
NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...
- EC读书笔记系列之17:条款41、42、43、44、45、46
条款41 了解隐式接口与编译器多态 记住: ★classes和templates都支持接口和多态 ★对classes而言接口是显式的(explicit),以函数签名为中心.多态则是通过virtual函 ...
- EC读书笔记系列之11:条款20、21
条款20 宁以pass-by-reference-to-const替换pass-by-value 记住: ★尽量以pass-by-reference-to-const替换pass-by-value.前 ...
- EC读书笔记系列之8:条款13、14、15
条款13 以对象管理资源 记住: ★为防止资源泄漏,请使用RAII对象,它们在构造函数中获得资源并在析构函数中释放 ★两个常被使用的RAII classes分别是tr1::shared_ptr和aut ...
- EC读书笔记系列之6:条款11 在operator=中处理自我赋值
记住: ★确保当对象自我赋值时operator=有良好行为.有三种方法:比较“来源对象”和“目标对象”的地址.精心周到的语句顺序.以及copy-and-swap技术 ★确定任何函数若操作一个以上对象, ...
随机推荐
- jQuery中设置form表单中action值的方法
jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈 html代码: <form id="myFormId&quo ...
- A Game with Colored Balls
题目链接 题意: 给一个长度为n的字符串,每次删除字母同样切连续的串,假设有多个,删除最左边的.最长的串.每次删除输出串的字母,每一个字母的下标(1-n) N (1 ≤ N ≤ 106),串仅仅包含r ...
- Android Service组件(1)
android service 和其他服务一样,并没有实际运行的界面,它运行在android 后台.一般通过service为应用程序提供服务(比如,从Internet下载文件,控制音乐播放器等).Se ...
- ArrayList、Vactor以及LinkList的区别
ArrayList:底层数据结构是数组结构,你就可以把它看成是一个可变大小的且只能装对象的数组.因为数组有索引(角标)所以ArrayList的查询速度快,而添加删除元素速度稍慢.因为,你每删除或者添加 ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Ubuntu package managerment tools
Visual demostration References Understanding differences between dpkg and apt-get/aptitude tools. A ...
- Linux系统学习笔记:文件描述符标志
文件描述符标志的概念 文件描述符标志(目前就只有一个close-on-exec): 它仅仅是一个标志,当进程fork一个子进程的时候,在子进程中调用了exec函数时就用到了这个标志.意义是执行exec ...
- 编译安装apache2.4
一.编译安装apache2.4Apache官方说:与Apache 2.2.x相比,Apache 2.4.x提供了很多性能方面的提升,包括支持更大流量.更好地支持云计算.利用更少的内存处理更多的并发等. ...
- Android 模块化编程之引用本地的aar
转: http://www.stormzhang.com/android/2015/03/01/android-reference-local-aar/ 随着项目越来越多,代码的复用就变得异常重要,这 ...
- requireJS到底是什么?
1.requireJS是让js代码模块化:而且js之间的依赖关系,不再依靠script标签的顺序,可以加载不阻塞 2.requireJS加载js的方法:<script data-main=&qu ...