Search code examples
c++compiler-constructionclass-members

Is it possible to manually calculate the byte-offset of a class member?


That is, what is the standard a compiler uses to generate a class? For example, let's say that I have class C with members x, y, and z, and I want to know the offset of z within that class. Can I just add up the data-type sizes of the other members, like I would for a structure?


Solution

  • No You cannot.
    Compilers are free to align the members as they chose to.It is an implementation detail of the compilers.

    If you're working with a POD, then you can use the offsetof macro.

    But If working with a Non-POD, then I suppose there won't be any portable method to do so.