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技术 ★确定任何函数若操作一个以上对象, ...
随机推荐
- java集合类深入分析之Queue篇(Q,DQ)
简介 Queue是一种很常见的数据结构类型,在java里面Queue是一个接口,它只是定义了一个基本的Queue应该有哪些功能规约.实际上有多个Queue的实现,有的是采用线性表实现,有的基于链表实现 ...
- C# 整个网页保存成图片
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Import MySQL Dumpfile, SQL Datafile Into My Database
How can I import a MySQL dumpfile into my database? I'm using CentOS Linux 5 server. My old hosting ...
- oracle 日期to_char转换24小时制12小时制
1>以12小时制显示 SQL>select to_char(sysdate,'YYYY-MM-DD HH12:MI:SS AM')from dual; TO_CHAR(SYSDATE,'Y ...
- OC 数组
/*---------------------------创建数组------------------------------*/ //NSArray *array = [[NSArray alloc ...
- HTML6注册表单输入日志标题
一.找到元素. var d = document.getElementById("") var d = document.getElementsByName("" ...
- Linux命令--链接文件的那些事
linux 链接ln的使用 linux操作系统下ln的使用方式: ln [option] source_file dest_file #source_file是待建立链接文件的文件,dest_file ...
- Spring学习之切入点表达式
链接地址:http://jinnianshilongnian.iteye.com/blog/1415606
- mysql 批量更新
bs_user 表,我们叫他 bu表, 字段user_id,len_id, think_wellUser 表,我们简称为tw表,中的user_id ,len_id 其中tw表的user_id 是bu表 ...
- Scala基础入门-4
Scala学习——类 简单类和无参方法 class Counter { private var value = 0 // 必须初始化字段 def increment() { value += 1 } ...