Skip to content

Latest commit

 

History

History
37 lines (32 loc) · 726 Bytes

File metadata and controls

37 lines (32 loc) · 726 Bytes
#include <iostream>
using std::cin; using std::cout; using std::endl; using std::cerr;
#include <string>
using std::string;

class HasPtr {
public:
    HasPtr(const string &s = string()) :
        ps(new string(s)), i(0) {};
    HasPtr(const HasPtr &);
    const string &get() const { return *ps;}
    HasPtr &operator=(const HasPtr&);
private:
    string *ps;
    int i;
};

HasPtr::HasPtr(const HasPtr &s) :
    ps(new string(*(s.ps))), i(s.i){};

HasPtr&
HasPtr::operator=(const HasPtr &hp) {
    string *tmp = new string(*hp.ps);
    delete ps;
    ps = tmp;
    i = hp.i;
    return *this;
}

int main() {
    HasPtr hp1("this is");
    HasPtr hp2 = hp1;
    cout << hp1.get() << "\n" << hp2.get() << endl;
}