ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Inheritance 에서 copy operator & assignment operator
    C++ 2014. 2. 15. 07:17

    Class에서 pointer를 사용하여 Heap Memory를 동적으로 할당하는 경우copy operator를 반드시 정의 해주어야 한다.
    마찬가지로 Assignment Operator 사용도 마찬가지로 꼭 정의를해주는 것이 좋다.
    이런 문제는 부모 자식의 상속관계에서도 반드시 고려해주어야 하는 것이 있다.

    1. 자식 class에서 Copy Operator를 정의하는 경우에는 반드시 부모 Class의Copy Operator도 정의를 하고 호출을 해주어야 한다.
    2. 자식 class에서Assignment Operator를 정의하는 경우에는 반드시 부모 Class의Assignment Operator도 정의를 하고 호출을해주어야 한다

    Class Super
    {
    Super();
    Super(const Super& inSuper);
    Super operator=(const Super& inSuper);
    }

    Class Child : public Super
    {
    Child ();
    Child (const Child& inChild) : Super(InChild);
    Child operator=(const Child& inChild) : Super(InChild)
    }

    Child::operator=(const Child& inChild)
    {
    if (&inChild==this) return *this;
    Super::operator=(&inChild); // 부모의 operator call

    }

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

    Vector constructor and descructor  (0) 2014.02.17
    STL Container 선택  (0) 2014.02.17
    Up-casting and Object Slicing  (0) 2014.02.14
    Polymophism의 사용  (0) 2014.02.14
    Overriding과 Virtual  (0) 2014.02.14
Designed by Tistory.