《Effective C++ 》学习笔记——条款12
***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************
二、Constructors,Destructors and Assignment Operators
Rule 12:Copy all parts of an object
规则12:复制对象时勿忘其每个成分
1.编译器的复仇!
首先,让我们了解一个词汇:copying
函数,这个包含:copy构造函数 和 copy assignment操作符。
之前的条款提到了。假设我们不做不论什么行动,编译器会自己主动为你生成copying函数,对类中每个成分进行复制。
假设你自己声明这些。就相当于对编译器说,你不须要它多管闲事。编译器就会"很生气"
因此假设你的copying函数一定出错的情况下,它都不会提醒你错误,它就是想看你笑话!
比方,以下这个样例:
void logCall(const std::string& funcName);
class Customer {
public:
...
Customer(const Customer& rhs);
Customer& operator=( const Customer& rhs);
...
private:
std::string name;
}
Customer::Customer( const Customer& rhs) : name(rhs.name)
{
logCall("Customer copy constructor");
}
Customer& Customer::operator=( const Customer& rhs)
{
logCall("Customer copy assignment operator");
name = rhs.name;
return *this;
}
上面这些东西都非常正常,但假设加一个类成员变量?
class Date { ... };
class Customer {
public:
...<span style="white-space:pre"> </span>// 与之前一样
private:
std::string name;
Date lastTransaction;
};
这时候的 copying函数 运行的是 局部拷贝 ,后加的 Data类型。并没有复制。
这样的情况下,生气的编译器 就不会告诉你这个错误,所以。要加入一个新东西,就须要更改对应的copying函数。
2.最突出的情况——继承
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声明的成员变量。PriorityCustomer是派生类。它里面还包括着基类Customer的成员变量,而这些没有被复制。
这是很严重的问题,编译器不会提醒你,所以假设出错。Wow!
出大事了。
3.填坑吧!
不论什么时候,仅仅要你承担起为 derived class撰写 copying函数的责任。必须非常小心的复制它基类的成分。
但这些成分往往是 private ,所以无法直接訪问,这就须要让 派生类 的copying函数调用对应的 基类函数:
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs ) : Customer(rhs),priority(rhs.priority)
{
logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& PriorityCustomer::operator=( const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment operator");
Customer::operator=(rhs);
priority = rhs.priority;
return *this;
}
本条款所说的复制每个成分,。就是说当你编写一个 copying 函数,请确保:
<1> 复制全部local成员变量
<2> 调用全部 基类 内的适当 copying函数
噢,对了还有两点要注意,不能由于避免代码反复而:
① 令copy assignment操作符 调用 copy构造函数
② 令 copy构造函数 调用 copy assignment操作符
通常,假设怕这两者代码反复,你能够通过建立一个新的private成员函数,把同代码写在里面。然后copy assignment 操作符 和 copy构造函数 调用它。
4.请记住
★ Copying函数应该确保复制“对象内的全部成员变量” 及 “全部 base class 成分”
★ 不要尝试以某个 copying函数实现 还有一个 copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。
***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************
《Effective C++ 》学习笔记——条款12的更多相关文章
- Effective C++学习笔记 条款07:为多态基类声明virtual析构函数
一.C++明确指出:当derived class对象经由一个base class指针被删除,而该base class带着一个non-virtual析构函数,其结果未定义——实际执行时通常发生的是对象的 ...
- Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝
一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...
- Effective C++学习笔记 条款05:了解C++默默编写并调用的哪些函数
一.如果用户没有提供构造函数.copy构造函数.copy assignment操作符和析构函数,当且仅当这些函数被需要的时候,编译器才会帮你创建出来.编译器生成的这些函数都是public且inline ...
- Effective C++学习笔记 条款04:确定对象被使用前已先被初始化
一.为内置类型对象进行手工初始化,因为C++不保证初始化它们. 二.对象初始化数据成员是在进入构造函数用户编写代码前完成,要想对数据成员指定初始化值,那就必须使用初始化列表. class A { pu ...
- Effective C++学习笔记 条款02:尽量以const,enum,inline替换 #define
尽量使用const替换 #define定义常量的原因: #define 不被视为语言的一部分 宏定义的常量,预处理器只是盲目的将宏名称替换为其的常量值,导致目标码中出现多分对应的常量,而const定义 ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
随机推荐
- 【转】MySQL存储引擎中的MyISAM和InnoDB区别详解
转自:http://www.jb51.net/article/62457.htm MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Ac ...
- Ajax——异步基础知识(三)
封装异步请求 1.将函数作为参数进行使用 2.因为获取数据是在一个注册事件中获取的,所以只有事件触发的时候才会调用此函数 <!DOCTYPE html> <html lang=&qu ...
- SQL基本操作——declare if lese while
declare --第一种 declare @i int set @i= (select COUNT(*) from t8) select @i --第二种 declare @i int select ...
- jQuery怎么去掉标签的hover效果
今天项目中遇到jquery去掉hover效果的问题,开始以为直接unbind(“hover”)就可以搞定,可是实际验证这个方法并没有作用,正确的使用方法应该是下面这样: /* 这种方法是新增的,在老的 ...
- #1003 Max Sum
http://acm.hdu.edu.cn/showproblem.php?pid=1003 给你一串数列,让你求出其中 一段连续的子数列 并且 该子数列所有数字之和最大,输出该子数列的和.起点与终点 ...
- id拼接保存到单个字段后作为表连接的查询条件
SELECT q.id, concat(q. NAME) qname, d.id did, d. NAME FROM question_po q LEFT JOIN data_configuratio ...
- adjtimex和时钟的几个概念tick,freq,ppm,jiffies
adjtimex使用 今天遇到一个ntp的同步问题.服务器上配置好了ntpd,在启动前也手动进行过同步,但是过段时间ntpq查询发现服务器即便能选出同步服务器,但是系统的时间偏差越来越大. 服务器上实 ...
- 模板中tempname与class区别
前言 在分析traits编程之前, 我们需要对模板参数类型tempname和class有一定的了解, 要明白他们在哪些方面不同, 哪些方面相同, 这样才能对体会到traits编程的核心. 如果你已经明 ...
- 洛谷——P4014 分配问题
P4014 分配问题 题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij .试设计一个将 nn 件工作分配给 nn 个人做的分配方案, ...
- JAVA经典题--死锁案例
死锁原理: 两个线程相互等待对方释放同步监视器 例子程序: public class TestDeadLock implements Runnable { public int flag = 1; s ...