I have an array class I grabbed off of a website that gives an example of a move constructor. How would one implement this move constructor in an example program however? I feel like I understand the function definition, but I have no idea how one would make use of this in a program.
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;
};