13.2

13.22

#include 

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;
    }
    ~HasPtr(){
    delete ps;
    }
private:
    std::string *ps;
    int i;
};

你可能感兴趣的:(C++,Primer第五版练习)