Detailed explanation of copy structure and mobile structure

Keywords: C++

Before C++11, the copy control of objects was determined by three functions: Copy Constructor, Copy Assignment operator and Destructor.

After C++11, two new functions are added: Move Constructor and Move Assignment operator.

give an example:

#include <iostream>
using namespace std;

class A {
public:
    int x;
    A(int x) : x(x)
    {
        cout << "Constructor" << endl;
    }
    A(A& a) : x(a.x)
    {
        cout << "Copy Constructor" << endl;
    }
    A& operator=(A& a)
    {
        x = a.x;
        cout << "Copy Assignment operator" << endl;
        return *this;
    }
    A(A&& a) : x(a.x)
    {
        cout << "Move Constructor" << endl;
    }
    A& operator=(A&& a)
    {
        x = a.x;
        cout << "Move Assignment operator" << endl;
        return *this;
    }
};

A GetA()
{
    return A(1);
}

A&& MoveA(A& a)
{
    return std::move(a);
}

int main()
{
    cout << "-------------------------1-------------------------" << endl;
    A a(1);
    cout << "-------------------------2-------------------------" << endl;
    A b = a;
    cout << "-------------------------3-------------------------" << endl;
    A c(a);
    cout << "-------------------------4-------------------------" << endl;
    b = a;
    cout << "-------------------------5-------------------------" << endl;
    A d = A(1);
    cout << "-------------------------6-------------------------" << endl;
    A e = std::move(a);
    cout << "-------------------------7-------------------------" << endl;
    A f = GetA();
    cout << "-------------------------8-------------------------" << endl;
    A&& g = MoveA(f);
    cout << "-------------------------9-------------------------" << endl;
    d = A(1);
}

Please guess what the output of these nine lines is.
Here are the answers:

-------------------------1-------------------------
Constructor
-------------------------2-------------------------
Copy Constructor
-------------------------3-------------------------
Copy Constructor
-------------------------4-------------------------
Copy Assignment operator
-------------------------5-------------------------
Constructor
Move Constructor
-------------------------6-------------------------
Move Constructor
-------------------------7-------------------------
Constructor
Move Constructor
Move Constructor
-------------------------8-------------------------
-------------------------9-------------------------
Constructor
Move Assignment operator

Resolution:

Line 1, no doubt, calls the constructor.
Line 2 creates a new object b, initializes b with a, and therefore calls the copy constructor.
Line 3 creates a new object c, initializes c with a, and therefore calls the copy constructor.
Line 4 updates object b with the value of A. since there is no need to create a new object, the copy assignment operator is called.
Line 5 creates a new object D and initializes d with the temporary object A(1). Since the temporary object is an R-value, the move constructor is called.
Line 6 creates a new object e, initializes e with the value of a, but calls std::move(a) to convert the left value a to the right value, so the move constructor is called.
Line 7 creates a new object F, initializes f with the temporary object returned by the GetA() function, and calls the move constructor because the temporary object is an R-value. It is worth noting that the move constructor is called twice. The first time is before GetA() returns, A(1) moves and constructs a temporary object. The second is the temporary object movement construct f.
Line 8 does not create a new object or update any objects. It just binds the return value of MoveA() to the right value reference g. Therefore, neither the constructor nor the assignment operator is called.
Line 9 updates d with the temporary object A(1). Since there is no need to create a new object, the move assignment operator is called.

Posted by cueball2000uk on Sun, 28 Nov 2021 04:24:20 -0800