Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 626
XIAODI Avatar asked Dec 21 '25 21:12

XIAODI


1 Answers

Here's whats going on:

HasPtr h("hi mom");
HasPtr h2 = h;

h.ps ---> "hi mom"  <--- h2.ps

Then with the next statement, you change h2 but that doesn't affect h:

HasPtr h3("hi dad");
h2 = h3;

h2.ps ----> "hi dad" <--- h3.ps
 |
XXX changed
 |
"hi mom" <--- h.ps

The pointers are different instances that point to the same address. Example:

int a = 5, c = 1; 
int *p1 = &a, *p2 = &a;

Changing p1 (ie p1 = &c) won't affect p2.

like image 108
yizzlez Avatar answered Dec 23 '25 12:12

yizzlez