Why does the size of class in c++ depend on the public/private status of data members?

前端 未结 1 2010
故里飘歌
故里飘歌 2021-01-01 11:37

From what i know, the size of a class in c++ depends on the below factors -

  1. Size of all non-static data members.
  2. Order of data members.
  3. If by
相关标签:
1条回答
  • 2021-01-01 12:14

    The Itanium ABI uses the C++03 definition of POD to define classes that are "POD for the purposes of layout". Having private data members disqualifies a class from being an aggregate and therefore POD in C++03:

    A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor.

    Being a POD class disables tail padding reuse:

    The dsize, nvsize, and nvalign of these types are defined to be their ordinary size and alignment. These properties only matter for non-empty class types that are used as base classes. We ignore tail padding for PODs because an early version of the standard did not allow us to use it for anything else and because it sometimes permits faster copying of the type.

    Thus, in your first example, A is not a POD for layout purposes and its tail padding can be used for B::c, but in your second example, it is a POD, and its tail padding cannot be reused.

    0 讨论(0)
提交回复
热议问题