harttle.github.io icon indicating copy to clipboard operation
harttle.github.io copied to clipboard

2015/08/01/effective-cpp-12

Open utterances-bot opened this issue 7 years ago • 3 comments

Item 12:完整地拷贝对象 | Harttle Land

在一个成熟的面向对象的C++系统中,只有两种拷贝对象的方式:复制构造函数和赋值运算符。当重载拷贝函数时,首先要完整复制当前对象的数据(local data);然后调用所有父类中对应的拷贝函数。

https://harttle.land/2015/08/01/effective-cpp-12.html

utterances-bot avatar May 23 '18 02:05 utterances-bot

class Customer{
  string name;
public:
  Customer::Customer(const Customer& rhs): name(rhs.name){}
  Customer& Customer::operator=(const Customer& rhs){
    name = rhs.name;                     // copy rhs's data
    return *this;                        // see Item 10
  }  
};

这里为什么要写"Customer::",明明都在类内部了啊。

ghost avatar May 23 '18 02:05 ghost

确实不需要,已经去掉~

harttle avatar May 23 '18 04:05 harttle

对init没有好感。。

ghost avatar Dec 04 '18 03:12 ghost