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.

http://en.wikipedia.org/wiki/virtual_method_table


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

What is the difference between data design and data model(ERD) -

ios - Can NSManagedObject conform to NSCoding -