Getting the size of member variable

前端 未结 7 395
猫巷女王i
猫巷女王i 2020-12-15 17:34

If there is a POD structure, with some member variables, for example like this:

struct foo
{
   short a;
   int b;
   char c[50];
   // ...
};
7条回答
  •  隐瞒了意图╮
    2020-12-15 18:20

    You can create macro wrapper of what @Erik suggested as:

    #define SIZE_OF_MEMBER(cls, member) sizeof( ((cls*)0)->member )
    

    And then use it as:

    cout << SIZE_OF_MEMBER(foo, c) << endl;
    

    Output:

    50
    

    Demo : http://www.ideone.com/ZRiMe

提交回复
热议问题