虚继承
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 class Ap { 5 public: 6 Ap(int a) { 7 this->a = a; 8 cout << "constructor Ap " << a << endl; 9 } 10 virtual ~Ap() {} 11 12 int a; 13 } ; 14 15 class A : public Ap { 16 public: 17 A(int a) : Ap(a) { 18 cout << "constructor A " << a << endl; 19 } 20 virtual ~A() {} 21 22 virtual void F1() { 23 cout << "A F1" << endl; 24 } 25 26 virtual void F2() { 27 cout << "A F2" << endl; 28 } 29 } ; 30 31 class B1 : virtual public A { 32 public: 33 B1(int a) : A(a) { 34 cout << "constructor B1 " << a << endl; 35 } 36 virtual ~B1() {} 37 38 virtual void F1() { 39 cout << "B1" << endl; 40 } 41 } ; 42 43 class B2 : virtual public A { 44 public: 45 B2(int a) : A(a) { 46 cout << "constructor B2 " << a << endl; 47 } 48 virtual ~B2() {} 49 50 //virtual void F2() { 51 //cout << "B2" << endl; 52 //} 53 } ; 54 55 class C : public B1, public B2 { 56 public: 57 C(int a) : A(a), B1(a), B2(a) { 58 cout << "constructor C " << a << endl; 59 } 60 virtual ~C() {} 61 62 virtual void F2() { 63 a = 1; 64 cout << "C" << endl; 65 } 66 } ; 67 68 void F2(A *p) { 69 p->F2(); 70 } 71 72 void F1(A *p) { 73 p->F1(); 74 } 75 76 int main() { 77 A a(1); 78 B1 b1(2); 79 B2 b2(3); 80 C c(4); 81 82 cout << a.a << ' ' << b1.a << ' ' << b2.a << ' ' << c.a << endl; 83 F1(&c); 84 F2(&a); 85 F2(&b2); 86 F2(&c); 87 cout << a.a << ' ' << b1.a << ' ' << b2.a << ' ' << c.a << endl; 88 89 return 0; 90 }
记录一下代码,没有默认构造器的情况下,初始化需要把所有相关的虚函数加进去。
因为类A只有一个副本,所以最终只是初始化了一次。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 class A { 5 public: 6 A(int a) { 7 cout << "A construct" << endl; 8 } 9 ~A() { 10 cout << "A destruct" << endl; 11 } 12 } ; 13 14 class B : public A { 15 public: 16 B(int a) : A(a) { 17 cout << "B construct" << endl; 18 } 19 ~B() { 20 cout << "B destruct" << endl; 21 } 22 } ; 23 24 class C1 : virtual public B { 25 public: 26 C1(int a) : B(a) { 27 cout << "C1 construct" << endl; 28 } 29 ~C1() { 30 cout << "C1 destruct" << endl; 31 } 32 } ; 33 34 class C2 : virtual public B { 35 public: 36 C2(int a) : B(a) { 37 cout << "C2 construct" << endl; 38 } 39 ~C2() { 40 cout << "C2 destruct" << endl; 41 } 42 } ; 43 44 class D1 : virtual public C1, virtual public C2 { 45 public: 46 D1(int a) : B(a), C1(a), C2(a) { 47 cout << "D1 construct" << endl; 48 } 49 ~D1() { 50 cout << "D1 destruct" << endl; 51 } 52 } ; 53 54 class D2 : virtual public C1, virtual public C2 { 55 public: 56 D2(int a) : B(a), C1(a), C2(a) { 57 cout << "D2 construct" << endl; 58 } 59 ~D2() { 60 cout << "D2 destruct" << endl; 61 } 62 } ; 63 64 class E1 : virtual public D1, virtual public D2 { 65 public: 66 E1(int a) : B(a), C1(a), C2(a), D1(a), D2(a) { 67 cout << "E1 construct" << endl; 68 } 69 ~E1() { 70 cout << "E1 destruct" << endl; 71 } 72 } ; 73 74 class E2 : virtual public D1, virtual public D2 { 75 public: 76 E2(int a) : B(a), C1(a), C2(a), D1(a), D2(a) { 77 cout << "E2 construct" << endl; 78 } 79 ~E2() { 80 cout << "E2 destruct" << endl; 81 } 82 } ; 83 84 class F : virtual public E1, virtual public E2 { 85 public: 86 F(int a) : B(a), C1(a), C2(a), D1(a), D2(a), E1(a), E2(a) { 87 cout << "F construct" << endl; 88 } 89 ~F() { 90 cout << "F destruct" << endl; 91 } 92 } ; 93 94 int main() { 95 F obj(1); 96 97 return 0; 98 }
——written by LyonLys