Search code examples
c++sizeofmemory-layoutvptr

Determine the size of object without its virtual table pointers


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?


Solution

  • 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);
    };