Object move, move constructor, and move assignment operator

1. What is object movement

    Object movement is to extract some useful data from an object A that is no longer used. When building a new object B, there is no need to rebuild the data in the object, but it is extracted from the object A that is no longer used. Useful data is used when constructing object B.

2. Move constructor

1. Why do you need a move constructor?

    We know before that there are copy constructors and overloaded assignment operators, both of which can complete object data copy, but when an object data is very large, the cost will be very high. So now C++11 proposes a move constructor, which is used to solve this problem.

2. Demonstration of move constructor

    The following code demonstrates the move constructor:

#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() { // 默认构造函数
        cout << "Use MyClass()" << endl;
    }
    MyClass(const MyClass& other) { // 复制构造函数
        cout << "Use MyClass(const MyClass& other)" << endl;
    }
    MyClass& operator=(const MyClass& other) { // 重载赋值运算符
        cout << "Use MyClass& operator=(const MyClass& other)" << endl;
    }
    MyClass(MyClass&& other) { // 移动构造函数
        cout << "Use MyClass(MyClass&& other)" << endl;
    }
    ~MyClass() { // 析构函数
        cout << "MyClass Destoryed" << endl;
    }
};

MyClass getMyClass() {
    MyClass my1;
    return my1;
}

int main() {
    MyClass my1;
    MyClass my2 = my1;
    MyClass my3(std::move(my2));

    return 0;
}

    In the main() function, the first line calls the default constructor, the second line calls the copy constructor, and the third line uses the std::move function, which changes my2 from an lvalue to an rvalue, so the call is a move constructor. This example is just a simple look at the use of the move constructor. In fact, the process of "moving" should be implemented in the function.

3. Move assignment operator

    The move assignment operator is the same as the move constructor. It just changes the function parameter to an rvalue based on the assignment operator. The following is a simple example of using the move assignment operator:

#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() { // 默认构造函数
        cout << "Use MyClass()" << endl;
    }
    MyClass(const MyClass& other) { // 复制构造函数
        cout << "Use MyClass(const MyClass& other)" << endl;
    }
    MyClass& operator=(const MyClass& other) { // 重载赋值运算符
        cout << "Use MyClass& operator=(const MyClass& other)" << endl;
    }
    MyClass(MyClass&& other) noexcept { // 移动构造函数
        cout << "Use MyClass(MyClass&& other)" << endl;
    }
    MyClass& operator=(MyClass&& other) noexcept { // 移动赋值运算符
        cout << "Use MyClass& operator=(MyClass&& other)" << endl;
    }
    ~MyClass() { // 析构函数
        cout << "MyClass Destoryed" << endl;
    }
};

MyClass getMyClass() {
    MyClass my1;
    return my1;
}

int main() {
    MyClass my1;
    MyClass my2;
    my2 = std::move(my1);

    return 0;
}

 In the above code, I added the symbol noexcept at the end of the move constructor and move assignment operator functions, which is also

C++11 newly added keywords, its function is to inform the compiler that the move constructor and move assignment operator will not throw any exceptions (to improve the efficiency of the compiler). It's a habit, remember it!

4. Synthetic mobile operation

    Synthesized move operations mean that in some cases, the compiler automatically synthesizes move constructors and move assignment operators. For the synthesis problem, the summary is as follows:

  • If a class (MyClass) defines its own copy constructor, overloaded assignment operator, or destructor (one of these three), the compiler will not synthesize a move constructor and move assignment operator for it.

  • If a class does not provide a move constructor and a move assignment operator, it will automatically use a copy constructor and an overloaded assignment operator instead.

  • If a class does not define any of its own copy constructor, overloaded assignment operator, destructor, and every non-static member of the class can be moved, the compiler will synthesize the move constructor and move assignment operation for the class symbol.

Guess you like

Origin blog.csdn.net/hu853712064/article/details/130559622