当前位置: 移动技术网 > IT编程>开发语言>C/C++ > Effective C++ Item 12-复制对象时忽忘其每一个成分

Effective C++ Item 12-复制对象时忽忘其每一个成分

2018年10月25日  | 移动技术网IT编程  | 我要评论

冰与火之歌列王的纷争,兰州新闻,月光爱人伴奏

item 12-复制对象时忽忘其每一个成分(copy all parts of an object)

设计良好之面向对象(oo-system)会将对象的内部封装起来,只留两个函数负责将对象拷贝(复制),那便是带着适切名称的copy构造函数和copy assignment操作符,称它们为copying函数。

如果是"编译器生成版本"的行为:将被拷对象的所有成员变量都做一份拷贝。

如果是自己声明就要注意了。

如果是为"derived class撰写copying函数"的重大责任,必须很小心的复制其base class成分。那些成分往往是private,所以你无法直接访问它们,你应该让derived class的copying函数调用相应的base class函数。

ex:

class customer      //base class
{
    public:
        ...
        customer(const customer& rhs);
        customer& operator = (const customer& rhs);
        ...
    private:
        string name;
}
customer::customer(const customer& rhs):name(rhs.name);
customer::customer& operator=(const customer& rhs)
{
    name=rhs.name;
    return *this;
}

class prioritycustomer:public customer   //derived class
{
    public:
        ...
        prioritycustomer(const prioritycustomer& rhs);
        prioritycustomer& operator=(const prioritycustomer& rhs);
        ...
    private:
        int priority;
}
prioritycustomer::prioritycustomer(const prioritycustomer& rhs):customer(rhs),priority(rhs.priority);
prioritycustomer::operator=(const prioritycustomer& rhs)
{
    customer::operator=(rhs)   //对base class成分进行赋值动作
    priority=rhs.priority;
    return *this;
}

 

 

"复制每一个成分"即复制所有local成员变量,调用所有base classes内的适当的copying函数

令copy assignment操作符调用copy构造函数是不合理的,因为这就像试图构造一个已经存在的对象。

令copy构造函数调用copy assignment操作符也没有意义。

如果copy构造函数和copy assignment操作符有相近的代码,消除重复代码的做法是,建立一个新的成员函数给两者调用。这样的做法往往private,而且常被命名为init。这个策略可以安全消除copy构造函数和copy assignment操作符之间的代码重复。

 

请记住:

copying函数应该确保复制"对象内的所有成员变量"及所有"base class"成分

不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。
 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网