简介
学习一下C++里面继承相关的知识。
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #pragma pack(push,1) class A { public: A() { a = 0; } int a; virtual void fun() { std::cout << "A::fun()\n"; }; };
class B : public A { public: B() { a = 2; b = 2; } int b; virtual void fun() { std::cout << "B::fun()\n"; }; };
class C : public A { public: C() { a = 3; c = 3; } int c; virtual void fun() { std::cout << "C::fun()\n"; }; };
class D: public B,public C { public: virtual void fun() { std::cout << "D::fun()\n"; }; };
class B1 : virtual public A { public: B1() { a = 22; b = 22; } int b; virtual void fun() { std::cout << "b1::fun()\n"; }; };
class C1 : virtual public A { public: C1() { a = 33; c = 33; } int c; virtual void fun() { std::cout << "c1::fun()\n"; }; };
class D1 : public B1, public C1 { public: virtual void fun() { std::cout << "D::fun()\n"; }; }; #pragma pack(pop)
int main(int argn, char* argc[]) { std::cout << "Hello the world!" << std::endl; A a;
B b; C c; D d; std::cout << "a " << sizeof(a) << std::endl; std::cout << "b " << sizeof(b) << std::endl; std::cout << "c " << sizeof(c) << std::endl; std::cout << "d " << sizeof(d) << std::endl; d.fun();
std::cout << "d.B::a" << d.B::a << std::endl; std::cout << "d.C::a" << d.C::a << std::endl; std::cout << std::endl;
B1 b1; C1 c1; D1 d1; std::cout << "b1 " << sizeof(b1) << std::endl; std::cout << "c1 " << sizeof(c1) << std::endl; std::cout << "d1 " << sizeof(d1) << std::endl;
std::cout << "d1.B1::a " << d1.B1::a << std::endl; std::cout << "d1.C1::a " << d1.C1::a << std::endl; std::cout << std::endl; return 0; }
|
分析
在C++中,多态是通过虚函数的动态绑定来实现的。vptr(virtual pointer)和vtbl(virtual function table)。vptr指针是和object绑定。vtbl是和class绑定。
使用虚拟继承节约A类重复的数据结构。我们将A中填充一些内存块,将会看到使用非虚拟继承的类,将会膨胀的很厉害。