ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 참조자(&)
    C++/개념정리 2018. 12. 21. 12:04

    변수


    int main()

    {

        int num = 3;                     

        int &ref = num;                    // num의 주소를 ref가 참조(num 과 ref의 변수 주소는 동일)

        int *numAddress = #      // num의 주소를 넘김.

        

    /*

    cout<<*numAddress<<endl;    // 3 반환.

    cout<<numAddress<<endl;     // num의 주소 반환. ex) 0x7ffe13550e84

    cout<<&numAddress<<endl;  // numAddress의 주소 반환. ex)0x7ffe13550e88

         */

        cout<<&num<<endl<<&ref<<endl;

        cout<<numAddress<<endl;

        return 0;

    }


    0x7ffe13550e84                                                                                                                                                                    
    0x7ffe13550e84                                                                                                                                                                    
    0x7ffe13550e84



    함수(&)


    #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;                // this 객체의 주소가 가리키는 값 반환.

        }

        SelfRef& ShowTwoNumber()

        {

            cout<<num<<endl;

            return *this;

        }

    };



    int main()

    {

        SelfRef obj(3);

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

        

        obj.ShowTwoNumber();

        ref.ShowTwoNumber();

        

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

        return 0;

    }


    객체생성                                                                                                                                                                          
    5                                                                                                                                                                                 
    5                                                                                                                                                                                 
    6                                                                                                                                                                                 

    함수(*)

    #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()
    {
        SelfRef obj(3);
        SelfRef *ref=obj.Adder(2);
        
        obj.ShowTwoNumber();
        ref->ShowTwoNumber();
        
        ref->Adder(1)->ShowTwoNumber()->Adder(2)->ShowTwoNumber();
        return 0;
        
    }


    객체생성                                                                                                                                                                          
    5                                                                                                                                                                                 
    5                                                                                                                                                                                 
    6                                                                                                                                                                                 




















    728x90

    'C++ > 개념정리' 카테고리의 다른 글

    this  (0) 2018.12.27
    friend  (0) 2018.12.21
    const  (0) 2018.12.21
    복사 생성자  (0) 2018.12.21
    #포인터 개념  (0) 2018.12.18

    댓글

Designed by Tistory.