C++ 상속과 패딩

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

상속과 패딩

  • 일반적인 구조체와 마찬가지로 패딩이 끼게 된다.

    • 패딩이 없는 예제 (int, float)

      #include <iostream>
      
      class Mother
      {
        int i_;
      
      public:
        Mother(const int & i_in = 0)
          : i_(i_in)
        {
          std::cout << "Mother construction\n";
        }
      };
      
      class Child : public Mother
      {
        float d_;
      
      public:
        Child()
          : d_(1.0), Mother(1024)
        {
          std::cout << "Child construction\n";
        }
      };
      
      int        main()
      {
        using namespace std;
      
        cout << sizeof(Mother) << endl;
        cout << sizeof(Child) << endl;
      }
      
      /* stdout
      4
      8
      */
    • 4바이트 패딩 예제 (int, double)

      #include <iostream>
      
      class Mother
      {
        int i_;
      
      public:
        Mother(const int & i_in = 0)
          : i_(i_in)
        {
          std::cout << "Mother construction\n";
        }
      };
      
      class Child : public Mother
      {
        double d_;
      
      public:
        Child()
          : d_(1.0), Mother(1024)
        {
          std::cout << "Child construction\n";
        }
      };
      
      int        main()
      {
        using namespace std;
      
        cout << sizeof(Mother) << endl;
        cout << sizeof(Child) << endl;
      }
      
      /* stdout
      4
      16
      */
    • 끝에 패딩이 4바이트 더 붙은 예제 (int, double, int)

      #include <iostream>
      
      class Mother
      {
        int i_;
      
      public:
        Mother(const int & i_in = 0)
          : i_(i_in)
        {
          std::cout << "Mother construction\n";
        }
      };
      
      class Child : public Mother
      {
        double d_;
        int    tmp_;
      
      public:
        Child()
          : d_(1.0), Mother(1024)
        {
          std::cout << "Child construction\n";
        }
      };
      
      int        main()
      {
        using namespace std;
      
        cout << sizeof(Mother) << endl;
        cout << sizeof(Child) << endl;
      }
      
      /* stdout
      4
      24
      */
저작자표시 (새창열림)

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

C++ 함수 오버라이딩  (0) 2021.03.20
C++ 상속과 접근 지정자  (0) 2021.03.20
C++ 유도된 클래스들의 소멸 순서  (0) 2021.03.19
C++ 유도된 클래스들의 생성 순서  (0) 2021.03.19
C++ 상속 Teacher-Student 예제  (0) 2021.03.19
'C++/Class' 카테고리의 다른 글
  • C++ 함수 오버라이딩
  • C++ 상속과 접근 지정자
  • C++ 유도된 클래스들의 소멸 순서
  • C++ 유도된 클래스들의 생성 순서
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • 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 (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 상속과 패딩
상단으로

티스토리툴바