1.多重继承的问题三

多重继承可能产生多个虚函数表Slide2.PNG

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. class BaseA
  6. {
  7. public:
  8. virtual void funcA()
  9. {
  10. cout << "BaseA::funcA()" << endl;
  11. }
  12. };
  13. class BaseB
  14. {
  15. public:
  16. virtual void funcB()
  17. {
  18. cout << "BaseB::funcB()" << endl;
  19. }
  20. };
  21. class Derived : public BaseA,public BaseB
  22. {
  23. };
  24. int main()
  25. {
  26. Derived d;
  27. BaseA* pa = &d;
  28. BaseB* pb = &d;
  29. //BaseB* pbb = (BaseB*)pa;
  30. BaseB* pbb = dynamic_cast<BaseB*>(pa); //解决方案
  31. cout << "sizeof(Derived) = " << sizeof(d) << endl; //有两个虚函数表指针
  32. cout << "Using pa to call funcA...." << endl;
  33. pa->funcA();
  34. cout << "Using pa to call funcB...." << endl;
  35. pb->funcB();
  36. cout << "Using pa to call funcB...." << endl;
  37. pbb->funcB(); //!!!!
  38. return 0;
  39. }

需要进行强制类型转换,c++中推荐使用新式类型转换关键字!!!
解决方案:dynamic_cast

Slide4.PNG
需要进行强制转换的对象是带有虚函数的类,则采用dynamic_cast

2.正确的使用多重继承的方式

工程开发中的“多重继承”方式
单继承某个类+ 实现多个接口
Slide5.PNG

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Base
  5. {
  6. protected:
  7. int mi;
  8. public:
  9. Base(int i)
  10. {
  11. mi = i;
  12. }
  13. int getI()
  14. {
  15. return mi;
  16. }
  17. bool equal(Base* obj)
  18. {
  19. return (this == obj);
  20. }
  21. };
  22. class Interface1
  23. {
  24. public:
  25. virtual void add(int i) = 0;
  26. virtual void minus(int i) = 0;
  27. };
  28. class Interface2
  29. {
  30. public:
  31. virtual void multiply(int i) = 0;
  32. virtual void divide(int i) = 0;
  33. };
  34. class Derived : public Base, public Interface1, public Interface2
  35. {
  36. public:
  37. Derived(int i) : Base(i)
  38. {
  39. }
  40. void add(int i)
  41. {
  42. mi += i;
  43. }
  44. void minus(int i)
  45. {
  46. mi -= i;
  47. }
  48. void multiply(int i)
  49. {
  50. mi *= i;
  51. }
  52. void divide(int i)
  53. {
  54. if( i != 0 )
  55. {
  56. mi /= i;
  57. }
  58. }
  59. };
  60. int main()
  61. {
  62. Derived d(100);
  63. Derived* p = &d;
  64. Interface1* pInt1 = &d;
  65. Interface2* pInt2 = &d;
  66. cout << "p->getI() = " << p->getI() << endl; // 100
  67. pInt1->add(10);
  68. pInt2->divide(11);
  69. pInt1->minus(5);
  70. pInt2->multiply(8);
  71. cout << "p->getI() = " << p->getI() << endl; // 40
  72. cout << endl;
  73. cout << "pInt1 == p : " << p->equal(dynamic_cast<Base*>(pInt1)) << endl; //强制类型转换
  74. cout << "pInt2 == p : " << p->equal(dynamic_cast<Base*>(pInt2)) << endl;
  75. return 0;
  76. }

单继承多接口的方式,加上dynamic_cast强制转化完成多继承的设计的,有了dynamic_cast,编译器会重新计算指针的值

3.一些有用的工程建议

—-先继承自一个父类,然后实现多个接口
—-父类中提供equal()成员函数,判断对象是否指向当前
—-equal()成员函数用于判断指针是否指向当前对象

  1. bool equal(Base* obj)
  2. {
  3. return (this == obj);
  4. }

—-与多重继承相关的强制类型转换用dynamic_cast完成

小结
1.多重继承中可能出现多个虚函数表指针
2.与多重继承相关的强制类型转换用dynamic_cast完成
3.工程开发中采用“单继承多接口”的方式使用多继承
4.父类提供成员函数用于判断指针是否指向当前对象