c++

std::move, RValue, LValue, 이동 생성자

에브리피플 2015. 1. 7. 12:28

class Base

{

public:

int a;

int b;

Base(){

}

Base( int a, int b )

{

this->a = a;

this->b = b;

}

Base( Base&& other )

{

a = other.a;

b = other.b;


other.a = 0;

other.b = 0;

}

};

struct Person

{

int age;

std::string name;

};

int _tmain(int argc, _TCHAR* argv[])

{

Base base2;

base2.a = 10;

base2.b = 5;

Base base(std::move(base2));

       // std::move를 써야 이동 생성자가 호출됨

     // 이동 생성자 정의안하면 말짱꽝


printf("move \n");

printf("%d %d %d %d", base.a, base.b, base2.a, base2.b );

Person person1;

Person& rPerson = person1;


person1.age = 10;

rPerson.name = "Mike";


printf("person\n");

std::cout<< person1.name.c_str() << ", " << rPerson.age <<std::endl;


int nValue = 10;

int& nValue2 = nValue; // L-Value


// 컴파일 에러 int&& nValue3 = nValue;

int&& nValue3 = 7; // R-Value