这经常发生在更改代码的时候,当有自己的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. C语言复合字面量的使用

    C99添加的特性,复合字面量(composite literal).一旦熟悉并使用,便会体会到简洁强大的表达. 所谓字面量就是固定数值的表示.数值和字符串类型都有字面量的表达.如: // 100, 1 ...

  2. cmake编译时遇到的问题解决

    编译cmake首先须要gcc环境,能够运行 gcc --version命令看看. 假设没有,能够使用yum或从cd中进行安装,此处是在虚拟机中从cd中进行安装.将cd链接到虚拟机都会吧,此处略去,.. ...

  3. Keil - 编译错误总结 01

    Keil 编译 STM32project,出现下述错误. 并且.   Options for Target  -> Output   -  Browse  Information 选项无法勾选. ...

  4. WorkFlow介绍及用法

    WorkFlow介绍及用法 说起workflow大家肯定都不陌生,这里简单介绍一下salesforce中什么情况下使用workflow. 当你分配许多任务,定期发送电子邮件,记录修改时,可以通过自动配 ...

  5. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

  6. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

  7. OpenLayers学习笔记4——使用jQuery UI实现測量对话框

    OpenLayers学习最好的方式就是跟着其自带的演示样例进行学习,另外对web前端的开发设计要了解,慢慢积累,这样在一般的小项目中应该是足够用了. 本篇參照量測demo实现对话框形式的量測,抛砖引玉 ...

  8. php 汉字转拼音 [包含20902个基本汉字+5059生僻字]

    原文:php 汉字转拼音 [包含20902个基本汉字+5059生僻字] 昨天在转换拼音的时候发现个bug,有好多字都无法转换,不过也不能怪他,毕竟人家的库才8k,应该只有常用的.无奈上网找了下,发现一 ...

  9. If you pay peanuts,you get monkeys

    英文原文:Before you send an email to contact a web developer, please read this… 做为一名开发者,我收到很多关于开发新 web 应 ...

  10. 在IIS上发布Web(使用VS2005)

    最近想在IIS上发布网站,弄了一下午.遇到很多问题,幸运的是都一一解决了,现在把解决问题的过程分享出来: 安装好IIS后,在VS2005上写了个网站(新建-->网站-->ASP.NET网站 ...