lundi 27 juillet 2015

c++ copy control issue

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class HasPtr{
 public:
    //constructor accepts a string
    HasPtr(const string &s = string()) : ps(new string(s)), i(0), use(new size_t(1)) {}  
    //copy constructor
    HasPtr(const HasPtr &h) : ps(h.ps), i(h.i), use(h.use) { ++*use; }
    //copy assignment operator 
    HasPtr &operator=(const HasPtr &h)
    {
        ++*h.use;
        if(--*use == 0)
        {
            delete ps;
            delete use;
        }
        ps = h.ps;
        i = h.i;
        use = h.use;

        return *this;
    }
    ~HasPtr()
    {
        if(--*use == 0)
        {
            delete ps;
            delete use;
        }
    }

//private:
    string *ps;
    int i;

    size_t *use;
};


int main()
{
    HasPtr h("hi mom");
    HasPtr h2 = h;
    HasPtr h3("hi dad");
    h2 = h3;

    cout << "h: " << *h.ps << endl;
    cout << "h2: " << *h2.ps << endl;
    cout << "h3: " << *h3.ps << endl;
}

The output is:

h: hi mom
h2: hi dad
h3: hi dad

I think the output should be:

h: hi dad
h2: hi dad
h3: hi dad

The reason why I think the output should like above:

h is "hi mom", h2 shares h1, so h2 is "hi mom", h3 is "hi dad", and I think h2 = h3 changed h1 either because h2 shares h1, but it's not.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire