ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Rvalue reference (우측값 참조)
    C++ 2014. 2. 28. 11:13

    C++에서 좌항 lvalue는 변수나 변수의 주소등을 갖는다.

    그에 비해 우항은 상수값이나 임시 객체같은 것들이 위치할 수가 있다.

    C++11에서 제공하는 우측값 참조는 상수나 임시 객체를 사용할 때 적용이 되는 개념이다.

    • 임시 객체 생성시
    • 복사 생성자
    • 대입 연산자

    Int& I = 2; // error

    Int&& I = 2;// OK

     

    int incr(int &num)

    {

        cout << "int incr(int &num)"<< endl;

        return ++num;

    }

     

    int incr(int &&num)

    {    

        cout << "int incr(int &&num)"<< endl;

        return ++num;

    }

     

    Void main()

        int a = 3, b=5;

        int&& c=a+b;

        cout << "c = " << c << endl;

     

        cout << incr(a) << endl;

        cout << incr(a+b) << endl;

        cout << incr(999) << endl;

        cout << incr(++a) << endl;

    가장 많이 우측값 참조가 사용되는 경우는 move constructor move assignment이다.

    move constructor 임시 객체가 생성되는 경우에 임시 객체의 값이 죄측으로 넘겨지고 임시 객체는 소멸하여 효울성을 높인다. 만일 이를 사용하지 않으면 복사 생성자가 생성되었다가 소멸하는 불필요한 메모리가 생성, 소멸되게 된다.

    Move assignment 역시 좌측 객체의 원본 데이타를 모두 삭제하고 임시 객체에 의해서 생성된 데이타를 얃은복사를 해서 효율성을 높인다.

    #include "Fruit.h"

     

     

    Fruit::Fruit(int id, int count)

    {

        cout << "constructor"<< endl;

     

        this->id=id;

        this->mCount=count;

        this->mCell = new char* [mCount];

        for (int i=0;i<mCount;++i)

            mCell[i] = new char[id];

    }

     

     

    Fruit::~Fruit(void)

    {

        cout << "Destructor"<< endl;

     

        for (int i=0;i<mCount;++i)

            delete []mCell[i];

        delete []mCell;

        mCell = nullptr;

    }

     

    Fruit::Fruit(const Fruit& src)

    {

        cout << "Copy constructor"<< endl;

        id=src.id;

        mCount=src.mCount;

        mCell=new char* [mCount];    

     

        for (int i=0;i<mCount;++i) {

            mCell[i] = new char[id];

            memcpy(mCell[i],src.mCell[i],id);

        }

    }

     

    Fruit::Fruit(Fruit&& src)

    {

        cout << "Move constructor"<< endl;

     

        id=src.id;

        mCell=src.mCell;    

        mCount=src.mCount;

     

        src.id=0;

        src.mCell=nullptr;    

        src.mCount=0;

    }

     

    Fruit& Fruit::operator=(const Fruit& rhs)

    {

        cout << "assignment operator"<< endl;

        if (&rhs==this) return *this;

     

        for (int i=0;i<mCount;++i)

            delete []mCell[i];

        delete []mCell;

        mCell = nullptr;

     

        id=rhs.id;

        mCount=rhs.mCount;

        mCell=new char*[mCount];

     

        for (int i=0;i<mCount;++i) {

            mCell[i] = new char[id];

            memcpy(mCell[i],rhs.mCell[i],id);

        }

     

        return *this;

    }

     

    Fruit& Fruit::operator=(Fruit&& rhs)

    {

        cout << "assignment move operator"<< endl;

        if (&rhs==this) return *this;

     

        for (int i=0;i<mCount;++i)

            delete []mCell[i];

        delete []mCell;

        mCell=nullptr;

     

        id=rhs.id;

        mCell=rhs.mCell;    

        mCount=rhs.mCount;

     

        rhs.id=0;

        rhs.mCell=nullptr;    

        rhs.mCount=0;

        return *this;

     

    }

     

    'C++' 카테고리의 다른 글

    Object Pool  (0) 2014.03.09
    Dynamic Memory Allocation and Memory Leak  (0) 2014.03.02
    Rambda Expression  (0) 2014.02.26
    string class operator overloding  (0) 2014.02.26
    Character array literal  (0) 2014.02.25
Designed by Tistory.