***************************************转载请注明出处: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的更多相关文章

  1. Effective C++学习笔记 条款07:为多态基类声明virtual析构函数

    一.C++明确指出:当derived class对象经由一个base class指针被删除,而该base class带着一个non-virtual析构函数,其结果未定义——实际执行时通常发生的是对象的 ...

  2. Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝

    一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...

  3. Effective C++学习笔记 条款05:了解C++默默编写并调用的哪些函数

    一.如果用户没有提供构造函数.copy构造函数.copy assignment操作符和析构函数,当且仅当这些函数被需要的时候,编译器才会帮你创建出来.编译器生成的这些函数都是public且inline ...

  4. Effective C++学习笔记 条款04:确定对象被使用前已先被初始化

    一.为内置类型对象进行手工初始化,因为C++不保证初始化它们. 二.对象初始化数据成员是在进入构造函数用户编写代码前完成,要想对数据成员指定初始化值,那就必须使用初始化列表. class A { pu ...

  5. Effective C++学习笔记 条款02:尽量以const,enum,inline替换 #define

    尽量使用const替换 #define定义常量的原因: #define 不被视为语言的一部分 宏定义的常量,预处理器只是盲目的将宏名称替换为其的常量值,导致目标码中出现多分对应的常量,而const定义 ...

  6. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  7. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  8. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  9. 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 ...

随机推荐

  1. UNIX环境高级编程--2

    UNIX标准及实现 ISO C: 国际标准化组织(International Organization for standardization , ISO)ISO C标准的意图是提供C程序的可移植性, ...

  2. Unity通过指定摄像机截屏

    简介 介于照抄网上之前的截图教程,然后在实际应用过程中出现了一些小小的问题,修正了一下下,特此分享一下 PS:代码在后面 原理 原理很简单,就是将一个相机的内容渲染到一个贴图上,然后将贴图保存为图片 ...

  3. mysql和java的时间对应关系

    引用:http://blog.csdn.net/xinghuo0007/article/details/51500923 MySQL(版本:5.1.50)的时间日期类型如下: datetime 8by ...

  4. Stanford coursera Andrew Ng 机器学习课程第二周总结(附Exercise 1)

    Exercise 1:Linear Regression---实现一个线性回归 重要公式 1.h(θ)函数 2.J(θ)函数 思考一下,在matlab里面怎么表达?如下: 原理如下:(如果你懂了这道作 ...

  5. nagios 安装pnp4nagios插件

    Naigos install pnp4nagios 绘图插件 原文地址:http://www.cnblogs.com/caoguo/p/5022230.html [root@Cagios ~]# yu ...

  6. HDU_1698_Just a Hook_线段树区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. Python 之数据类型

    # Numbers(数字) # int(有符号整型) # long(长整型[也可以代表八进制和十六进制]) # float(浮点型) # complex(复数) # String(字符串) # Lis ...

  8. PHP 之用证书对数据进行签名、验签、加密、解密

    /** * 对数据进行签名 * $data = 'If you are still new to things, we’ve provided a few walkthroughs to get yo ...

  9. 学不好Python?我们分析看看正确的学习方法是什么-马哥教育

    提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...

  10. 基础:Post和Get区别

    1.get是从服务器上获取数据,post是向服务器传送数据.2.在客户端, get方式在通过URL提交数据,数据在URL中可以看到:post方式,数据放置在HTML HEADER内提交3.对于get方 ...