harttle.github.io
harttle.github.io copied to clipboard
2015/08/01/effective-cpp-12
Item 12:完整地拷贝对象 | Harttle Land
在一个成熟的面向对象的C++系统中,只有两种拷贝对象的方式:复制构造函数和赋值运算符。当重载拷贝函数时,首先要完整复制当前对象的数据(local data);然后调用所有父类中对应的拷贝函数。
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::",明明都在类内部了啊。
确实不需要,已经去掉~
对init没有好感。。