为什么移动构造函数不调用?

时间:2017-04-02 05:07:28

标签: c++ logic copy-constructor

请查看以下示例代码:

    class testo
{
public:
    testo()
    {
        cout << " default " << endl;
    }

    testo(const testo & src)
    {
        cout << "copy " << endl;
    }
    testo(const testo && src)
    {
        cout << "move" << endl;
    }
    testo & operator=(const testo & rhs)
    {
        cout << " assigment" << endl;
        return *this;
    }
    testo & operator= (const testo && rhs)
    {
        cout << "move" << endl;
    }

};

这是我的功能和主要代码:

testo nothing(testo & input)
{
return input;
}

int main ()
{
testo boj1 ;
testo obj2(nothing(obj1) );
return 1;
}

当我编译并运行此代码时,我希望看到:

default    // default constructor

copy       // returning from the function

move       // moving to the obj2

但是当代码执行时,它只显示:

default 

copy

编译器是Visual C ++ 2015

1 个答案:

答案 0 :(得分:0)

移动签名应该定义为yii,而不是T&&。虽然语言中没有任何内容阻止你宣布某些内容T const &&,但实际上没有任何意义:你的意思是从这个对象移开,但它是T const &&因此不能改变它的状态?这是一个矛盾的术语。