Again about rvalues

12 August 2010

You can use rvalues for creating move semantics - remove copy constructors for temporaries. Example:

#include <iostream>

class A
{
public:
    A()
    {
        x = new int[1];
        x[0] = 1;
        std::cout << "newn";
    }
    ~A()
    {
        delete[] x;
    }

    A(const A& a)
    {
        x = new int[1];
        x[0] = a.x[0];
        std::cout << "newn";
    }

   /* A(A&& a) //move constructor
    {
        x = a.x;
        a.x = 0;
    }*/
private:
    int *x;
};

A GetA()
{
    A a;
    return a;
}
Share on Facebook Share on Google Plus Share on LinkeIn

I'm in social networks

GitHub

LinkedIn

Google

Search:

Tags: