effective c++ 条款12 copy all parts of an object
这经常发生在更改代码的时候,当有自己的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的更多相关文章
- 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 ...
- 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)
NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...
- Effective C++ 条款12
复制对象时,勿忘其每个成分 作者在本节条款提醒我们,在多重继承的情况下进行copy或者copy assignment 的operator=的编写时,一定要考虑base 类部分数据的初始化后者复制. 对 ...
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...
- Effective C++ 条款12:复制对象时勿忘其每一个成分
void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...
- [EffectiveC++]item12:copy all parts of an object
在小书C++中,4.2.2 派生类的构造函数和析构函数的构造规则(103页) 在定义派生类对象时,构造函数执行顺序如下: 基类的构造函数 对象成员的构造函数 派生类的构造函数.
- 《Effective C++ 》学习笔记——条款12
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- [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};枚举类 ...
随机推荐
- 计算机视觉与模式识别代码合集第二版two
Topic Name Reference code Image Segmentation Segmentation by Minimum Code Length AY Yang, J. Wright, ...
- 【iOS】Swift字符串截取方法的改进
字符串截取方法是字符串处理中经常使用的基本方法.熟悉iOS的朋友都知道在基础类的NSString中有substringToIndex:,substringFromIndex:以及substringWi ...
- win7如何清理图标缓存
rem 关闭Windows外壳程序explorer taskkill /f /im explorer.exe rem 清理系统图标缓存数据库 attrib -h -s -r "%userpr ...
- CMake Intro - CMakeLists.txt
Notes: directory structure: cmake, cmake/Tutorial, cmake/Tutorial/MathLibs 1. File lists in cmake/ ...
- android--手机桌面添加网址链接图标(解决方式)
这样的做法最普遍最简单: 1.新建一个android空项目: 2.在drawable文件夹下加入图标文件,如icon.png:在values文件夹下的strings.xml文件里添加名称.如websi ...
- CCEditBox/CCEditBoxImplIOS
#ifndef __CCEditBoxIMPLIOS_H__ #define __CCEditBoxIMPLIOS_H__ #include "cocos2d.h" #if (CC ...
- 下载jdk文件后缀是.gz而不是.tar.gz怎么办
用chrom浏览器下载了linux版的jdk,发现文件后缀是.gz,没看过这玩意,一打开,还是一个.gz文件,原本以为是新文件后缀呢.那个百度google啊. . ..最后都没发现有这方面的资料啊.. ...
- C#语言实现ArcGIS数据源重置之Set Data Source功能
1.须要:依据选择的Mxd路径和目标数据源路径进行重置数据源.此处以(.Mdb为例): 主要利用到的接口: (1)IMapDocument (2)IMapControl2 (3)IWor ...
- 基于Grunt的版本号构建系统新手教程
作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...
- hdu2845(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2845 题意:给你一个n*m的矩阵,每个位置有一定数量的豆子,如果你去map[x][y]位置上的豆子,则 ...