C++ 공변 반환형(Covariant Return Type)

2021. 3. 20. 17:43·C++/Class

공변 반환형(Covariant Return Type)

  • 포인터나 레퍼런스의 전달에서, 상속 관계의 클래스 반환 자료형에 관한 내용이다.

예제

  • 다음은 가상 멤버 함수 getThis로 받은 포인터를 통해 일반 멤버 함수 print를 호출하는 예제이다.

    #include <iostream>
    
    class A
    {
    public:
      void print() { std::cout << "A" << std::endl; }
    
      virtual A* getThis() 
      { 
        std::cout << "A::getThis()" << std::endl;
        return this; 
      }
    };
    
    class B : public A
    {
    public:
      void print() { std::cout << "B" << std::endl; }
    
      virtual B* getThis() 
      {
        std::cout << "B::getThis()" << std::endl;
        return this; 
      }
    };
    
    int        main()
    {
      using namespace std;
    
      A        a;
      B        b;
    
      A&    ref = b;
      b.getThis()->print();
      ref.getThis()->print();
    
      cout << typeid(b.getThis()).name() << endl;
      cout << typeid(ref.getThis()).name() << endl;
    }
    
    /* stdout
    B::getThis()
    B
    B::getThis()
    A
    class B *
    class A *
    */
    • stdout에서 볼 수 있듯이, ref.getThis()는 B::getThis()를 호출하고 B* 형태의 주소를 반환한다.

      • 하지만 ref의 자료형이 A 클래스의 레퍼런스이므로, A* 형태의 주소로 인식되어 A::print()가 실행되는 것이다.

      • typeid(ref.getThis()).name() 함수의 결과가 class A * 형태인 것으로도 유추할 수 있다.

저작자표시

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

C++ 정적 바인딩 (Static Binding)  (0) 2021.03.20
C++ 가상 소멸자  (0) 2021.03.20
C++ Override, Final  (0) 2021.03.20
C++ 가상 함수와 다형성 (virtual)  (0) 2021.03.20
C++ 다형성의 기본 개념  (0) 2021.03.20
'C++/Class' 카테고리의 다른 글
  • C++ 정적 바인딩 (Static Binding)
  • C++ 가상 소멸자
  • C++ Override, Final
  • C++ 가상 함수와 다형성 (virtual)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (316)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (19)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    citrix workspace
    백기선
    알림
    Solaris 10
    unix
    스프링 프레임워크 핵심 기술
    SunOS 5.1
    mspaint
    EXCLUDE
    dism
    SFC
    제외
    MacOS
    그림판
    java
    스프링
    윈도우 명령어
    logi options
    시스템 복구
    KakaoTalk
    맥북 카카오톡 알림 안뜸
    windows
    윈도우
    spring
    Windows 11
    vscode
    Workspace
    로지텍 마우스 제스처
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 공변 반환형(Covariant Return Type)
상단으로

티스토리툴바