Categories
Mastering Development

Why not destory after calling std::move? [closed]

As seen in the following, I can still print "3" after the code printf("%d\n", p1.val);. Isn’t the class p1 destroyed after std::move? code

The code is here:

class pointer
{
public:
    int val;
    int* my_pointer;

    pointer()
    {
        val = 0; 
        my_pointer = new int;
    }
    pointer(pointer&& other)
    {
        val = other.val;
        my_pointer = other.my_pointer;      
        other.my_pointer = nullptr;
    }
    ~pointer()
    {
        delete my_pointer;
        my_pointer = nullptr;
    }
};
int main()
{   
    pointer p1;
    p1.val = 3;
    *p1.my_pointer = 3;
    pointer p2(std::move(p1));
    printf("%d\n", p1.val);
    printf("%d\n", p2.val);
    return 0; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *