C++ 상속과 패딩

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

상속과 패딩

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

    • 패딩이 없는 예제 (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
      */
반응형
저작자표시 (새창열림)

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

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
'SW개발/C++' 카테고리의 다른 글
  • C++ 함수 오버라이딩
  • C++ 상속과 접근 지정자
  • C++ 유도된 클래스들의 소멸 순서
  • C++ 유도된 클래스들의 생성 순서
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바