【发布时间】:2017-04-02 05:07:27
【问题描述】:
请看下面的示例代码:
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
【问题讨论】:
-
请添加您用于测试课程的代码。
-
从移动构造函数/移动赋值中移除 const 限定符
-
对不起,我忘记了函数
-
测试记录(测试和输入){返回测试}
-
编译器很聪明。它看到没有任何东西的输出可以直接进入
obj2,而无需任何额外的麻烦,所以它确实将返回时的副本构建到obj2的初始化中。
标签: c++ logic copy-constructor