C++11 移动语义 (move semantics)

先看代码

#include <iostream>
#define PRINT_STRING(x)                                                                                                \
    std::cout << #x << " = ";                                                                                          \
    x.Print();                                                                                                         \
    std::cout << std::endl;

class String
{
  public:
    String() = default;
    String(const char *string)
    {
        std::cout << "Constructor" << std::endl;
        m_Size = strlen(string);
        m_Data = new char[m_Size];
        memcpy(m_Data, string, m_Size);
    }

    String(const String &other)
    {
        std::cout << "Copy Constructor" << std::endl;
        m_Size = other.m_Size;
        m_Data = new char[m_Size];
        memcpy(m_Data, other.m_Data, m_Size);
    }

    String(String &&other) noexcept
    {
        std::cout << "Move Constructor" << std::endl;
        m_Size = other.m_Size;
        m_Data = other.m_Data;

        other.m_Size = 0;
        other.m_Data = nullptr;
    }

    String &operator=(const String &other)
    {
        std::cout << "Copy Assignment" << std::endl;
        if (this != &other)
        {
            delete[] m_Data;
            m_Size = other.m_Size;
            m_Data = new char[m_Size];
            memcpy(m_Data, other.m_Data, m_Size);
        }
        return *this;
    }

    String &operator=(String &&other) noexcept
    {
        std::cout << "Move Assignment" << std::endl;
        if (this != &other)
        {
            delete[] m_Data;
            m_Size = other.m_Size;
            m_Data = other.m_Data;
            other.m_Size = 0;
            other.m_Data = nullptr;
        }
        return *this;
    }

    ~String()
    {
        std::cout << "Destructor" << std::endl;
        delete[] m_Data;
    }

    void Print()
    {
        for (uint32_t i = 0; i < m_Size; ++i)
        {
            printf("%c", m_Data[i]);
        }
        printf("\n");
    }

  private:
    uint32_t m_Size;
    char *m_Data;
};

int main()
{
    String string = "Hello";
    String dest = std::move(string); // operator= means Move Constructor here

    std::cout << "Before assignment-----------" << std::endl;
    PRINT_STRING(string);
    PRINT_STRING(dest);

    string = std::move(dest); // operator= means Move Assignment here, 
                              // which means the discrepancy between the two methods is 
                              // whether the object that calls the function (operator=) is existing or not.
    std::cout << "After reassignment-----------" << std::endl;
    PRINT_STRING(string);
    PRINT_STRING(dest);
}

把这段代码粘贴到自己的编译器中, 开启单步调试, 重点关注string和dest这两个对象下面的m_Data内存地址的变化

你就能了解到,为什么这么多人说,移动语义本质上就是"偷"了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值