Determine the size of object without its virtual table pointers

主宰稳场 提交于 2019-12-13 15:19:26

问题


Is there a generic way (not platform dependent) to get at compile time the size of a class object in the memory, without counting the vtable pointers?


回答1:


As you are asking for a portable way:

class MyClass
{
private:
  struct S 
  {
    DataMemberType1 dataMember1;
    ...
    DataMemberTypeN dataMemberN;
  } m;

public:
  static const size_t MemberSize = sizeof(S);
}; 



回答2:


Use sizeof on this class, it doesn't include size of the vtable just the pointer.



来源:https://stackoverflow.com/questions/28433973/determine-the-size-of-object-without-its-virtual-table-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!