C++ 가상 소멸자

2021. 3. 20. 17:45·SW개발/C++
반응형

가상 소멸자

  • virtual 키워드를 소멸자에도 붙일 수 있다.

예제

  • 가상 소멸자를 사용하지 않은 기본 예제

    #include <iostream>
    
    class Base
    {
    public:
      ~Base() { std::cout << "~Base()" << std::endl; }
    };
    
    class Derived : public Base
    {
      int *array_;
    
    public:
      Derived(const int& length)
      {
        array_ = new int[length];
      }
    
      ~Derived()
      {
        std::cout << "~Derived()" << std::endl;
        delete[] array_;
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Derived    derived(5);
    }
    
    /* stdout
    ~Derived()
    ~Base()
    */

  • 메모리 누수 예제

    #include <iostream>
    
    class Base
    {
    public:
      ~Base() { std::cout << "~Base()" << std::endl; }
    };
    
    class Derived : public Base
    {
      int *array_;
    
    public:
      Derived(const int& length)
      {
        array_ = new int[length];
      }
    
      ~Derived()
      {
        std::cout << "~Derived()" << std::endl;
        delete[] array_;
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Derived    *derived = new Derived(5);
      Base* base = derived;
    
      delete base;
    }
    
    /* stdout
    ~Base()
    */
  • 부모와 자식 클래스의 소멸자에 virtual 키워드를 붙이고, 자식 클래스의 소멸자에 override까지 붙여주면 좋다.

    #include <iostream>
    
    class Base
    {
    public:
      virtual ~Base() { std::cout << "~Base()" << std::endl; }
    };
    
    class Derived : public Base
    {
      int *array_;
    
    public:
      Derived(const int& length)
      {
        array_ = new int[length];
      }
    
      virtual ~Derived() override
      {
        std::cout << "~Derived()" << std::endl;
        delete[] array_;
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Derived    *derived = new Derived(5);
      Base* base = derived;
    
      delete base;
    }
    
    /* stdout
    ~Derived()
    ~Base()
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ 동적 바인딩 (Dynamic Binding)  (0) 2021.03.20
C++ 정적 바인딩 (Static Binding)  (0) 2021.03.20
C++ 공변 반환형(Covariant Return Type)  (0) 2021.03.20
C++ Override, Final  (0) 2021.03.20
C++ 가상 함수와 다형성 (virtual)  (0) 2021.03.20
'SW개발/C++' 카테고리의 다른 글
  • C++ 동적 바인딩 (Dynamic Binding)
  • C++ 정적 바인딩 (Static Binding)
  • C++ 공변 반환형(Covariant Return Type)
  • C++ Override, Final
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 가상 소멸자
상단으로

티스토리툴바