13.6 重载了operator=,对象是相同类型的运算符 当用一个对象对另一个对象赋值的时候使用 类似于基础类型的赋值,把符号右边的赋值给左边 当类内没有定义的时候,编译器会合成自己的版本
13.7 会发生浅拷贝,这种情况是不允许的
13.8
#include <string>
class HasPtr {
public:
HasPtr(
const std::
string &s =
std::
string()) : ps(
new std::
string(s)), i(
0) { }
HasPtr(
const HasPtr& hp) : ps(
new std::
string(*hp.ps)), i(hp.i) { }
HasPtr&
operator=(
const HasPtr& rhs){
if(
this=&rhs)
return this;
delete ps;
ps=
new string (*rhs.ps);
i=rhs.i;
return *
this;
}
private:
std::
string *ps;
int i;
};
转载请注明原文地址: https://ju.6miu.com/read-658487.html