ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Copy Constructor vs Assignment operator
    C++ 2014. 2. 12. 23:22

    유사해 보이지만 의외로 이를 구분하는 방법은 쉽다.

    하나는 constructor 이고 Asssignment는 operator이다.

    즉 Copy는 객체가 없는 상태에서 새로 객체가 생성이 되는 것이고

    Assignment는 이미 있는 객체에 다른 객체의 무엇을 복사해주는 것이다.

    class First

    {

    private:

        int ma;

    public:

        First(void);

        First(int x);

     

        ~First(void);

        First& operator = (const First& rhs);

        const int getA() ;

    };

    class Second :

        public First

    {

    private:

        int mb;

    public:

        Second(void);

        Second(int i);

        const int getB();

     

        ~Second(void);

    };

    Main()

    {

        First f1, f2;

        Second s1, s2;

     

    s1 = 3; // Second default assignment operator

    f1 = s1; // First assignment operator

    First f3(5); // First constructor

     

    f1 = First(); // 임시 객체 생성후default assignment operator 수행

    s1 = f1 ; // error

    }

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

    Overriding과 Virtual  (0) 2014.02.14
    Inheritance에서 virtual destructor  (0) 2014.02.14
    Constructor 와 메모리  (0) 2014.02.12
    Cast 연산자  (0) 2014.01.22
    RTTI : Run-time type information 사용예  (0) 2014.01.18
Designed by Tistory.