虚拟继承

   
    从意义上来看,一个SleeperSofa没有沙发和床两种重量, 如此的继承不是真实的现实世界描述。进一步分析可得,床和沙发都是家具的一种,凡家具都有重量,所以通过分解来考察其关系,如图1所示。


图1 床和沙发的分解

  1. //**   Sample1.cpp  **//
  2. #include<iostream.h>
  3. class Furniture
  4. {
  5. public:
  6.     Furniture(){};
  7.     void SetWeight(int i)
  8.     {
  9.         weight =i;
  10.     }
  11.     
  12.     int GetWeight()
  13.     {
  14.         return weight;
  15.     }      
  16. protected:
  17.     int weight;
  18. };

  19. class Bed : public Furniture
  20. {
  21. public:
  22.     Bed(){};
  23.     void Sleep()
  24.     {
  25.         cout << "Sleeping...//n";
  26.     }
  27. };
  28. class Sofa : public Furniture
  29. {
  30. public :
  31.     Sofa(){};
  32.     void WatchTV()
  33.     { 
  34.         cout << "Watching TV.//n";
  35.     }
  36. };
  37. class SleeperSofa : public Bed, public Sofa
  38. {
  39. public:
  40.     SleeperSofa() : Sofa(), Bed(){};
  41.   void FoldOut()
  42.     {
  43.         cout << "Fold out the sofa.//n";
  44.     }
  45. };
  46. void main()
  47. {
  48.     SleeperSofa ss;
  49.     ss.SetWeight(20);     //编译出错!模糊的SetWeight成员

  50.     Furniture* pF;
  51.     pF =(Furniture*)&ss;      //编译出错!模糊的Furniture*

  52.     cout << pF->GetWeight() << endl;
  53. }

    因为SleeperSofa不是直接继承Furniture,而是从Bed和Sofa组合继承,Bed和Sofa又是各继承自Furniture,所以完整的SleeperSofa对象内存布局如图2所示:



图2 完整SleeperSofa对象内存布局

   

    这里一个Sleepersofa包括一个完整的Bed,随后还有一个完整的Sofa,后面还有一个 Sleepersofa特有的东西。SleeperSofa中的每一个子对象都有它自己的Furniture部分。因为每个子对象继承自Furniture,所以一个SleeperSofa包含两个Furniture对象,实际上的继承层次如图3所示:



图3 SleeperSofa的实际继承关系


  编译Sample1.cpp时,不知道SetWeight()属于哪一个Furniture成员,指向Furniture的指针也不知道究竟指哪一个Furniture。这就是为什么Sample1.cpp编译通不过的原因SleeperSofa只需一个Furniture,所以我们希望它只含一个Furniture拷贝,同时又要共享Bed和Sofa的成员函数与数据成员,C++实现这种继承结构的方法称为虚拟继承(virtual inheritance)。

    下面是虚拟继承的代码:

  1. //**   Sample2.cpp  **
  2. #include<iostream.h>
  3. class Furniture
  4. {
  5. public:
  6.     Furniture(){};
  7.     void SetWeight(int i)
  8.     {
  9.         weight =i;
  10.     }
  11.     
  12.     int GetWeight()
  13.     {
  14.         return weight;
  15.     }      
  16. protected:
  17.     int weight;
  18. };
  19. class Bed : virtual public Furniture
  20. {
  21. public:
  22.     Bed(){};
  23.     void Sleep()
  24.     {
  25.         cout <<"Sleeping...//n";
  26.     }
  27. };
  28. class Sofa : virtual public Furniture
  29. {
  30. public :
  31.     Sofa(){};
  32.     void WatchTV()
  33.     { 
  34.         cout <<"Watching TV.//n";
  35.     }
  36. };
  37. class SleeperSofa : public Bed, public Sofa
  38. {
  39. public:
  40.     SleeperSofa() : Sofa(), Bed(){};
  41.   void FoldOut()
  42.     {
  43.         cout <<"Fold out the sofa.//n";
  44.     }
  45. };
  46. void main()
  47. {
  48.     SleeperSofa ss;
  49.     ss.SetWeight(20);
  50.     Furniture* pF;
  51.     pF =(Furniture*)&ss;
  52.     cout <<pF->GetWeight() <<endl;
  53. }

    运行结果为:20

  在Bed和Sofa继承Furniture中加上virtual关键字,这相当于说,“如果还没有Furniture类,则加入一个Furniture拷贝,否则就用有的那一个”。此时一个Sleepersofa在内存中的布局见图4:



图4 虚拟继承的SleepeiSofa内存布局


  在虚拟继承的情况下,应用程序main()中引用GetWeight()不再模糊,我们得到了真正的图1所示的继承关系。

    虚拟继承的虚拟和虚拟函数的虚拟没有任何关系。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值