为数组类实现移动构造函数(右值引用)
Implementing a move constructor(rvalue reference) for an array class
我有一个数组类,我从一个网站上抓取了一个移动构造函数的例子。然而,如何在示例程序中实现这个move构造函数呢?我觉得我理解了函数的定义,但我不知道如何在程序中使用它。
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{}
ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{}
// move constructor, how does this come in handy?
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( other._size )
{
other._p_vals = NULL;
}
// copy constructor
ArrayWrapper (const ArrayWrapper& other)
: _p_vals( new int[ other._size ] )
, _size( other._size )
{
for ( int i = 0; i < _size; ++i )
{
_p_vals[ i ] = other._p_vals[ i ];
}
}
~ArrayWrapper ()
{
delete [] _p_vals;
}
private:
int *_p_vals;
int _size;
};
如何在示例程序中实现这个移动构造函数呢?
我认为你的代码已经表明了这一点。
我觉得我理解了函数的定义,但是我不知道如何在程序中使用它。
只需使用几种触发移动的方法之一。例如:
ArrayWrapper aw;
ArrayWrapper aw2 = std::move(aw);
甚至:
ArrayWrapper foo()
{
ArrayWrapper aw;
//...
return aw;
}
// ...
ArrayWrapper aw2 = foo();
请注意,在最后一种情况下,编译器可能会忽略对move构造函数的调用。
在与复制构造函数相同的情况下使用,但仅当被复制的表达式是右值时使用。右值通常指向一个临时对象。例如,如果您有一个函数foo
,它按值返回ArrayWrapper
,那么调用该函数的表达式将是一个右值。
ArrayWrapper aw = foo();
这里,ArrayWrapper
对象将由foo
返回的临时对象构造。选择move构造函数重载是因为右值引用实参绑定到右值表达式。
move构造函数通常会使要移动的对象处于有效但不确定的状态。
相关文章:
- 在类构造函数中传递对外部函数的引用
- 在 c++ 中将变量作为结构构造函数中的引用传递
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 移动构造函数和右值引用
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- C++:右值引用构造函数和复制省略
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- 引用构造函数时链接失败
- 为什么调用转发引用构造函数而不是复制构造函数?
- 为什么使用已删除的右值引用构造函数?
- 对 std::Optional 的转发引用构造函数的约束
- 模板是否应该为不同类型的参数制作非 Rvalue 引用构造函数/赋值
- 我是否应该对非引用构造函数参数手动调用move
- 使用左值引用错误地调用了Rvalue引用构造函数
- C++未定义的引用构造函数错误
- 调用左值引用构造函数而不是右值引用构造函数
- C++引用构造函数语法
- 不能引用构造函数的地址