为什么移动构造函数不调用
why move Constructor is not call?
请看下面的示例代码:
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
移动签名应该定义为T&&
,而不是T const &&
。虽然语言中没有任何内容阻止你声明T const &&
的东西,但实际上没有任何意义:你的意思是离开这个对象,但它const
,因此不能改变它的状态?这是一个术语的矛盾。
相关文章:
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 在模板化类的构造函数中调用构造函数
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 我不明白在这个例子中什么时候调用构造函数
- 调用c++构造函数的不同方法
- 调用构造函数与将内联常量定义为默认参数
- 如何通过 Rust FFI 调用C++构造函数?
- "new"运算符是否总是调用构造函数?
- 无法调用构造函数
- 使用 "()" 调用构造函数不同于"{}"
- 确定是调用构造函数还是强制转换运算符的因素