***************************************转载请注明出处: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. cocos2dx实现单机版三国杀(一)

    首先需要一个UI交互类 GameUI   -layer 一个游戏驱动类,负责游戏逻辑的循环 暂时定为GameScene- scene GameScene obj 调用update 更新游戏,addch ...

  2. [ SCOI 2009 ] 最长距离

    \(\\\) \(Description\) 一个\(N\times M\)的网格图中有一些坏点,图是四联通的. 你至多可以拿走\(K\)个坏点,求拿走后联通的点对中欧几里得距离最大是多少. \(N, ...

  3. SAS进阶《深入解析SAS》之开发多语言支持的SAS程序

    SAS进阶<深入解析SAS>之开发多语言支持的SAS程序 1. 多语言支持的应用程序是指该程序在世界给第使用时,其能够处理的数据,以及处理数据的方式.信息展现的方式都符合当地的语言.文化习 ...

  4. enc28j60网卡驱动模块添加进linux内核,Kconfig,Makefile配置过程

    这里是要把http://www.cnblogs.com/hackfun/p/6260396.html中的enc28j60网卡驱动模块,添加到2.6.22.6内核中,这个模块代码不需要任何修改.只需要在 ...

  5. jQuery——事件操作

    事件绑定 1.简单事件绑定 $("button").click(function () {})//可重复绑定,不会被层叠 2.bind():不推荐使用 $("button ...

  6. (原创)如何对APP服务端进行压力测试

    版权声明:本文为原创文章,转载请先联系并标明出处 APP性能测试分为客户端性能测试和服务端性能测试,客户端的性能测试主要是针对启动快慢.耗电量.耗流量.内存使用等指标进行评估,目前主流的APP客户端性 ...

  7. iOS crash log 解析 symbol address = stack address - slide 运行时获取slide的api 利用dwarfdump从dsym文件中得到symbol

    概述: 为什么 crash log 内 Exception Backtrace 部分的地址(stack address)不能从 dsym 文件中查出对应的代码? 因为 ASLR(Address spa ...

  8. ngFor 循环带索引

    *ngFor="let item of userList,let i = index"   或者 *ngFor="let item of userList index a ...

  9. 微信小程序——weui的使用

    使用在根目录中复制weui.wxss,app.wxss中引入 在weui.io中查看到自己想要的样式表后,到第二个网站复制代码,复制到自己的项目中即可 <!--pages/register/re ...

  10. [C#] 序列化实现对象的深拷贝

    //对象深拷贝 public static T Copy<T>(T oldObject) where T : class,new() { T newOrder = new T(); Mem ...