Effective C++ 条款12
复制对象时,勿忘其每个成分
作者在本节条款提醒我们,在多重继承的情况下进行copy或者copy assignment 的operator=的编写时,一定要考虑base 类部分数据的初始化后者复制。
对照一下代码:
class Cutsomer
{
……
private:
string name;
string telphone;
};
class PriorityCustomer:public Cutsomer
{
public:
PriorityCustomer()
{
cout<<"PriorityCustomer Ctor"<<endl;
}
PriorityCustomer(const PriorityCustomer& rhs)
:priority(rhs.priority)
{
cout<<"PriorityCustomer Copy Ctor"<<endl;
}
PriorityCustomer& operator=(const PriorityCustomer& rhs)
{
cout<<"PriorityCustomer assign operator"<<endl;
priority=rhs.priority;
return *this;
}
private:
int priority;
};
PriorityCustomer中的数据有下面
int priority;
string name;
string telphone;
而真正copy或者copy assignment的时候仅仅处理了int priority;
我们能够看到上面的代码中忽视了base类部分的数据的处理。这时改动代码例如以下:
PriorityCustomer(const PriorityCustomer& rhs)
:Cutsomer(rhs),priority(rhs.priority)
{
cout<<"PriorityCustomer Copy Ctor"<<endl;
}
PriorityCustomer& operator=(const PriorityCustomer& rhs)
{
cout<<"PriorityCustomer assign operator"<<endl;
Cutsomer::operator=(rhs);
priority=rhs.priority;
return *this;
}
Effective C++ 条款12的更多相关文章
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...
- effective c++ 条款12 copy all parts of an object
这经常发生在更改代码的时候,当有自己的copy 赋值函数或者copy 构造函数时,编译器就不会维护这两个函数.导致发生遗忘. 可能出现的场景 class Customer { private: std ...
- Effective C++ 条款12:复制对象时勿忘其每一个成分
void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...
- [More Effective C++]条款22有关返回值优化的验证结果
(这里的验证结果是针对返回值优化的,其实和条款22本身所说的,考虑以操作符复合形式(op=)取代其独身形式(op),关系不大.书生注) 在[More Effective C++]条款22的最后,在返回 ...
- More Effective C++ 条款0,1
More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...
- 《Effective C++ 》学习笔记——条款12
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Effective C++ 条款11,12 在operator= 中处理“自我赋值” || 复制对象时不要忘记每一个成分
1.潜在的自我赋值 a[i] = a[j]; *px = *py; 当两个对象来自同一个继承体系时,他们甚至不需要声明为相同类型就可能造成别名. 现在担心的问题是:假如指向同一个对象, ...
- Effective C++ 条款08:别让异常逃离析构函数
1.别让异常逃离析构函数的原因 <Effective C++>第三版中条款08建议不要在析构函数中抛出异常,原因是C++异常机制不能同时处理两个或两个以上的异常.多个异常同时存在的情况下, ...
- Effective C++ -----条款28:避免返回handles指向对象内部成分
避免返回handles(包括reference.指针.迭代器)指向对象内部.遵守这个条款可增加封装性,帮助const成员函数的行为像个const,并将发生“虚吊号码牌”(dangling handle ...
随机推荐
- [Tools] Create your own mobile emulator device by using Chrome dev tool
Using the New Device Emulation Interface The Device Emulation interface changed a bit with the newer ...
- [Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object
In some cases your application might need to upload large amounts of data, such as files. Obviously ...
- Linux文件查找命令具体解释-which whereis find locate
原创BLog.转载请注明出处 http://blog.csdn.net/hello_hwc? viewmode=contents which命令 首先查看man which的说明 which - sh ...
- 软工视频总结Part Three
软件需求分析 任务 解决目标系统"做什么"问题 深入描写叙述软件的功能和性能 确定软件涉及到的约束和软件接口 定义软件的其它有效需求 特点 一致性.完整性.限时性.有效性.可验证性 ...
- Popupwindow 显示, 其它背景变暗。 并加上点击事件 ~ (用于记录)
public class MainActivity extends Activity implements OnClickListener { protected int mScreenWidth; ...
- [JZOJ 5905] [NOIP2018模拟10.15] 黑暗之魂(darksoul) 解题报告 (拓扑排序+单调队列+无向图基环树)
题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰 ...
- 判断一个整数是否是回文数C++实现 leetcode系列(九)
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- inline元素和inline-block元素的4px空白间距解决方案
实在不想写了,要吐了,看到一篇讲的比较全的文章,直接粘链接了 inline元素和inline-block元素的4px空白间距解决方案 出自脚本之家
- 你不知道的JavaScript博文参考书籍
you don't know js系列书籍是谷歌地图开发人员编写,内容非常好,四卷已收集齐全. 笔者打包上传到了CSDN,下载地址: http://download.csdn.net/detail/r ...
- HDU 2852 KiKi's K-Number【 树状数组 二分 】
题意:给出m个操作,0:是增加一个数,add(x,1)1:是删除一个指定的数,这个是看sum(x) - sum(x-1)是否为0,为0的话则不存在,不为0的话,则add(x,-1)2:是查询比x大的数 ...