C++ 11 - 在构造函数中移动基础数据类型?

San*_*lin 2 move-semantics c++11

我正在研究C++ 11中的移动语义,我很好奇如何在构造函数中移动基本类型,如boolean,integer float等.复合类型也像std :: string.

以下面的类为例:

class Test
{
public:
    // Default.
    Test()
        : m_Name("default"), m_Tested(true), m_Times(1), m_Grade('B')
    {
        // Starting up...
    }
    Test(const Test& other)
        : m_Name(other.m_Name), m_Times(other.m_Times)
        , m_Grade(other.m_Grade), m_Tested(other.m_Tested)
    {
        // Duplicating...
    }
    Test(Test&& other)
        : m_Name(std::move(other.m_Name)) // Is this correct?
    {
        // Moving...
        m_Tested = other.m_Tested; // I want to move not copy.
        m_Times = other.m_Times; // I want to move not copy.
        m_Grade = other.m_Grade; // I want to move not copy.
    }

    ~Test()
    {
        // Shutting down....
    }

private:
    std::string     m_Name;
    bool            m_Tested;
    int             m_Times;
    char            m_Grade;
};
Run Code Online (Sandbox Code Playgroud)

如何移动(不复制)m_Tested,m_Times,m_Grade.并且m_Name是否正确移动?感谢您的时间.

eca*_*mur 6

从prvalue或xvalue原语初始化和赋值原语与初始化或从左值原语赋值完全相同; 复制值并且源对象不受影响.

换句话说,你可以使用,std::move但它不会有任何区别.

如果你想改变源对象的值(0比如说),你必须自己做.