C++ POD struct inheritance? Are there any guarantees about the memory layout of derived members

后端 未结 3 1256
时光说笑
时光说笑 2020-12-10 01:18

Let\'s say, I have a struct RGB and I want to create struct RGBA, which inherits RGB:

struct RGB {
    unsigned char r         


        
3条回答
  •  庸人自扰
    2020-12-10 01:32

    It's easy to check for padding: Print sizeof(RGB) and sizeof(RGBA). If it's not 3 respective 4 then the structures are padded, and you need to remove it.

    As for if the member a comes after b, you can use offsetof to check each members offset. If the offset for a is one larger than the offset of b then a comes directly after b.

提交回复
热议问题