• C++11 move记录


    说到C++11新增的move,得先说一下左值和右值。
    比如int a = 1; int b = a;我们说在b=a中,a是左值,因为它有名字,在之后还可以访问到;
    int a = 1; int b = a + c;这里得a+c就是右值,它是一个临时变量,没有名字,所以之后无法对它进行访问。

    对于移动构造函数(move constructor)

    #include <cstring>
    #include <algorithm>
    
    class string
    {
        char* data;
    
    public:
    
        string(const char* p)
        {
            size_t size = std::strlen(p) + 1;
            data = new char[size];
            std::memcpy(data, p, size);
        }
        ~string()
        {
            delete[] data;
        }
    
        string(const string& that)
        {
            size_t size = std::strlen(that.data) + 1;
            data = new char[size];
            std::memcpy(data, that.data, size);
        }
         
    

    上述例程中的拷贝构造函数(copy constructor)是一个深拷贝,我们在堆上开辟新的内存空间,然后将that的数据一一拷贝过来。

    对于左值,使用拷贝构造函数是必须的。比如string a(x);,因为我们之后还可能继续访问x
    对于右值,比如string b(x+y);或者string c(some_function_returning_a_string());x+ysome_function_returning_a_string()这样的临时变量在之后不能被访问到,在下一个语句时就会被析构掉,那为何不好好利用起来呢?因为对于右值,我们不直接深拷贝堆数据,而是直接拷贝这个临时变量的数据指针。具体如下

    string(string&& that)   // string&& is an rvalue reference to a string
    {
        data = that.data;
        that.data = nullptr;
    }
    

    因为这并不是拷贝,所以称为移动构造(move constructor)。

    参考

    What is move semantics?

  • 相关阅读:
    09-2:跳台阶
    09:菲波那切数列
    08:旋转数组的最小值
    07:用两个栈实现队列
    06:重建二叉树
    05:从尾到头打印链表
    04:替换字符
    centos7安装Jenkins更改默认端口并配置Ldap服务器进行用户认证
    Jira配置openLdap服务器进行用户认证
    定时自动从FTP服务器取数据脚本
  • 原文地址:https://www.cnblogs.com/liuxin0430/p/12419209.html
Copyright © 2020-2023  润新知