#include <iostream>
using namespace std;
class a {
virtual int foo() {
return 0;
}
};
class b {
int foo() {
return 0;
}
};
int main() {
cout << sizeof(b) << endl;
cout << sizeof(a) << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出(使用g ++ 4.9,-O3):
1
8
Run Code Online (Sandbox Code Playgroud)
我认为尺寸的增加是由于增加了一个vpointer.但我认为编译器会发现a实际上并不是从任何东西派生或派生出来的,因此不需要添加vpointer?