#include "stdafx.h" class A { public: int i; A() { i=1; } }; class B: public A { public: int i; B() { i=2; } }; class C: public B { public: int i; C() { i=3; } }; int main(int argc, char* argv[]) { A a1; printf(" %d\n", a1.i); B a2; printf(" %d\n", a2.i); C a3; printf(" %d\n", a3.i); printf(" %d\n", ((B)a2).i); printf(" %d\n", ((B)a3).i); printf(" %d\n", ((C)a3).i); printf(" %d\n", sizeof(a1)); printf(" %d\n", sizeof(a2)); printf(" %d\n", sizeof(a3)); return 0; }
打印结果:
1
2
3
2
2
3
4
8
12