class hasptr
{
private:
string *ps;
int i;
int *use;
public:
//这种默认构造函数挺好的,即使没给值,也会自动给一个默认空值
hasptr(const string &s = string()) :ps(new string(s)), i(0), use(new int(0)) {};
//拷贝构造函数
hasptr(const hasptr &has) :ps(has.ps), i(has.i), use(has.use) { ++*use; };
//拷贝赋值运算符
hasptr &operator=(const hasptr &pp)
{
if (--*use == 0)
{
delete use;
delete ps;
}
++*pp.use;
use = pp.use;
ps = pp.ps;
i = pp.i;
return *this;
}
~hasptr() {
if (--*use == 0)
{
delete use;
delete ps;
}
}
};