zl程序教程

您现在的位置是:首页 >  后端

当前栏目

《Effective C++ 》学习笔记——条款12

C++笔记学习 12 Effective 条款
2023-09-11 14:15:00 时间

***************************************转载请注明出处: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********************************************