딸기우유중독 2018. 12. 27. 13:00

this

//class의 instance(객체)를 가리키는 pointer.(인스턴스의 메모리주소를 가리킴)


*this 

//class의 instance(객체)를 의미



#include <iostream>

using namespace std;


class SelfRef

{

private:

    int num;

public:

    SelfRef(int n) : num(n)

    {

        cout<<"객체생성"<<endl;

    }

    SelfRef& Adder(int n)

    {

        num+=n;

        return *this;

    }

    SelfRef& ShowTwoNumber()        // 반환형이 참조형

    {

        cout<<num<<endl;

        return *this;                         // 객체자신을 반환

    }

};


int main(void)


{

    SelfRef obj(3);

    SelfRef &ref=obj.Adder(2);    // obj객체를 참조.

    

    obj.ShowTwoNumber();

    ref.ShowTwoNumber();

    

    ref.Adder(1).ShowTwoNumber().Adder(2).ShowTwoNumber();

    return 0;

}


728x90