c++ - class with virtual functions takes more space -
there such code:
#include <iostream> class a{ int a; int fun(){} }; class b{ int a; virtual int fun(){} }; int main() { std::cout << sizeof(a) << " " << sizeof(b) << std::endl; std::cin.get(); return 0; }
the output is:
4 8
why class b 4 bytes bigger class a?
any class virtual function needs pointer vtable class. therefore, there hidden member that's size of pointer.
Comments
Post a Comment