练习13.22:
#include <iostream>
#include <string>
using namespace std;
class HasPtr
{
public:
//赋予string值
HasPtr(const std::string& s = std::string()) :
ps(new string(s)), i(0) { }
//像值的拷贝构造函数
HasPtr(const HasPtr& hp) :ps(new string(*hp.ps)),i(hp.i) { }
//像值的拷贝赋值运算符
HasPtr& operator=(const HasPtr&);
//析构函数
~HasPtr() { delete ps; }
private:
std::string* ps;
int i;
};
HasPtr& HasPtr::operator=(const HasPtr& ptr)
{
auto newp = new string(*ptr.ps);
delete ps;
ps = newp;
i = ptr.i;
return *this;
}