这经常发生在更改代码的时候,当有自己的copy 赋值函数或者copy 构造函数时,编译器就不会维护这两个函数。导致发生遗忘。

可能出现的场景

class Customer
{
private:
std::string name;
public:
Customer(const Customer& rhs):name(rhs.name){};
Customer& operator=(const Customer& rhs)
{
name = rhs.name;
return *this;
}
}

这一切都很美好直到

case 1:在代码维护时很常见

class Customer
{
private:
std::string name;
Date modifiedDate; // added
public:
Customer(const Customer& rhs):name(rhs.name){};
Customer& operator=(const Customer& rhs)
{
name = rhs.name;
return *this;
}
}

这个时候增加了ModifiedDate 却忘记修改拷贝构造函数和赋值函数

case 2:发生在继承的时候

class AdvancedCustomer:Customer
{
private:
int creditAmount;
public:
AdvancedCustomer(const AdvancedCustomer& rhs):creditAmount(rhs.creditAmount){};
AdvancedCustomer& operator=(const AdvancedCustomer& rhs)
{
creditAmount = rhs.creditAmount;
return *this;
}
}

这个时候只有继承类的成员被赋值,基类的成员却没有,解决方法是调用基类的相关函数

class AdvancedCustomer:Customer
{
private:
int creditAmount;
public:
AdvancedCustomer(const AdvancedCustomer& rhs): Customer(rhs) // added to initial member of base class
creditAmount(rhs.creditAmount){};
AdvancedCustomer& operator=(const AdvancedCustomer& rhs)
{
Customer::operator=(rhs); // added to initial member of base class
creditAmount = rhs.creditAmount; return *this;
}
}

effective c++ 条款12 copy all parts of an object的更多相关文章

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

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

  2. 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)

    NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...

  3. Effective C++ 条款12

    复制对象时,勿忘其每个成分 作者在本节条款提醒我们,在多重继承的情况下进行copy或者copy assignment 的operator=的编写时,一定要考虑base 类部分数据的初始化后者复制. 对 ...

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

    Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...

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

    void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...

  6. [EffectiveC++]item12:copy all parts of an object

    在小书C++中,4.2.2 派生类的构造函数和析构函数的构造规则(103页) 在定义派生类对象时,构造函数执行顺序如下: 基类的构造函数 对象成员的构造函数 派生类的构造函数.

  7. 《Effective C++ 》学习笔记——条款12

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  8. [More Effective C++]条款22有关返回值优化的验证结果

    (这里的验证结果是针对返回值优化的,其实和条款22本身所说的,考虑以操作符复合形式(op=)取代其独身形式(op),关系不大.书生注) 在[More Effective C++]条款22的最后,在返回 ...

  9. More Effective C++ 条款0,1

    More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...

随机推荐

  1. freemark换行输出

    <!--附件图片-->              <#if attatList? exists>       <#if (attatList?size>0)> ...

  2. Cocos2d-x 3.1.1 学习日志8--2分钟让你知道cocos2d-x3.1.1 文本类别

    实际上文本经常使用的三个,LabelTTF,LabelBMF和LabelAtlas.而他们使用非常相似.所以,你会只举一反三,非常快就能够掌握了. <span style="font- ...

  3. android水平循环滚动控件

    CycleScrollView.java package com.example.test; import android.content.Context; import android.graphi ...

  4. 用TinyXml2读取XML文件的一个简单Demo

    废话少说直接上代码,需要的人自然一看便懂,对于第一次接触TinyXml2的人来说还是有帮助的. <?xml version="1.0"?> <Table name ...

  5. android 在特殊应用的特殊功能,以帮助通信系统的问题

    在实际工程中的应用,进入一个特殊的应用后,系统的某个功能不能起作用. 当然,这个通信有非常多办法能够做到.笔者能够想到的至少有例如以下几种 1.利用property熟悉来实现,这种话须要添加一个特殊的 ...

  6. hdu4956 Poor Hanamichi

    解决暴力的直接方法.一个直接的推论x%11方法. 打表可以发现,以解决不同的情况都不会在很大程度上会出现. 所以从l暴力开始枚举.找到的第一个错误值输出要. 如果它超过r同样在美国发现-1. #inc ...

  7. RequireJS学习资料

    RequireJS学习资料汇总   入门系列 [1]阮一峰 RequireJS用法 [2]RequireJS入门指南 文档系列 [1]RequireJS中文文档 [2]RequireJS英文文档 代码 ...

  8. Django Form Media 阅读笔记

    ---恢复内容开始--- Form Media Rendering an attractive and easy-to-use Web form requires more than just HTM ...

  9. Python做的眼睛护士

    搞了两天终于搞定了,虽然还存在一点点小问题(窗口的显示位置应该设在(0,0)).但基本可以用了. 代码分两个部分.主界面和遮挡屏幕界面.主界面设置完时间后调用遮挡屏幕界面. 1.主界面(设置 工作时间 ...

  10. cocos2d-x快乐的做让人快乐的游戏3:cocos-2d 3.x中的物理世界

    Cocos2d-x 3.0+ 中全新的封装的物理引擎给了开发人员最大的便捷,你不用再繁琐与各种物理引擎的细节,全然的封装让开发人员能够更快更好的将物理引擎的机制加入�到自己的游戏中,简化的设计是从2. ...