Question about the move contructor

Here is my code, I got instance a, and b created by Move constructor base on a.
Class Move has one variable data.
I think "move" make b "point to" a, so "a.data" and "b.data" should have the same addr, but in my case, the addr is not same. Why this happens?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// C++ program with declaring the
// move constructor
#include <iostream>
#include <vector>
using namespace std;

// Move Class
class Move {

public:

    int* data;
    // Constructor
    Move(int d)
    {
        // Declare object in the heap
        data = new int;
        *data = d;
        cout << "Constructor is called for "
            << d << endl;
    };

    // Copy Constructor
    Move(const Move& source)
        : Move{ *source.data }
    {

        // Copying the data by making
        // deep copy
        cout << "Copy Constructor is called -"
            << "Deep copy for "
            << *source.data
            << endl;
    }

    // Move Constructor
    Move(Move&& source)
        : data{ source.data }
    {

        cout << "Move Constructor for "
            << *source.data << endl;
        source.data = nullptr;
    }


    // Destructor
    ~Move()
    {
        if (data != nullptr)

            // If pointer is not pointing
            // to nullptr
            cout << "Destructor is called for "
            << *data << endl;
        else

            // If pointer is pointing
            // to nullptr
            cout << "Destructor is called"
            << " for nullptr "
            << endl;

        // Free up the memory assigned to
        // The data member of the object
        delete data;
    }
};

// Driver Code
int main()
{
    // Vector of Move Class
    //vector<Move> vec;

    //// Inserting Object of Move Class
    //vec.push_back(Move{ 10 });
    //vec.push_back(Move{ 20 });
    Move a(10);
    cout << &(a.data) << endl;
    Move b = move(a);
    cout << &(b.data) << endl;
    return 0;
}
The issue is in main(). &(a.data), &(b.data) is the address of the .data member within the variable - not the memory address held within data. The address of the .data member will be different for each variable - but for move(), the memory pointed to will be the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iterator>

// Move Class
class Move {
public:
	int* data {};

	Move(int d) : data(new int { d }) {
		std::cout << "Constructor for " << d << '\n';
	};

	Move(const Move& source) : Move { *source.data } {
		std::cout << "Copy Constructor for -" << *source.data << '\n';
	}

	Move(Move&& source) noexcept : data { source.data } {
		std::cout << "Move Constructor for " << *source.data << '\n';
		source.data = nullptr;
	}

	~Move() {
		if (data)
			std::cout << "Destructor for " << *data << '\n';
		else
			std::cout << "Destructor for nullptr\n";

		delete data;
	}

	Move& operator=(Move) = delete;
};

int main() {
	Move a { 10 };

	std::cout << a.data << '\n';

	Move b { std::move(a) };

	std::cout << b.data << '\n';
}



Constructor for 10
000000000051D730
Move Constructor for 10
000000000051D730
Destructor for 10
Destructor for nullptr

Topic archived. No new replies allowed.