Implementing a move constructor(rvalue reference) for an array class

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;
};

Answers


How would one implement this move constructor in an example program however?

I think the code you have shows that already.

I feel like I understand the function definition, but I have no idea how one would make use of this in a program.

Just use one of the several ways to trigger a move. For instance:

ArrayWrapper aw;
ArrayWrapper aw2 = std::move(aw);

Or even:

ArrayWrapper foo()
{
    ArrayWrapper aw;
    //...
    return aw;
}

// ...

ArrayWrapper aw2 = foo();

Notice, that in this last case the compiler is likely to elide the call to the move constructor anyway.


It is used in the same situations that the copy constructor would be but only if the expression being copied from is an rvalue. An rvalue typically refers to a temporary object. So, for example, if you have a function foo that returns an ArrayWrapper by value, the expression that calls that function would be an rvalue.

ArrayWrapper aw = foo();

Here, the ArrayWrapper object will be constructed from the temporary object returned by foo. The move constructor overload is chosen because the rvalue reference argument binds to the rvalue expression.

A move constructor typically leaves the object it is moving from in an valid but indeterminate state.


Need Your Help

PHP curl form with no action and js events

javascript php forms curl

I'm trying to use curl to post some data and retrieve it from a website that isn't mine, the websites form doesn't seem to have a form action and the page doesn't reload when you submit the data and

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.