There is 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 is 4 bytes bigger than class A?
Any class with a virtual function needs a pointer to the vtable for that class. Therefore, there is a hidden member that's the size of the pointer.
http://en.wikipedia.org/wiki/Virtual_method_table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With