小讲下C++虚拟继承

来源:互联网 发布:终极算法 书评 编辑:程序博客网 时间:2024/05/01 19:29

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


图17-2 床和沙发的分解
    //**********************
    //**   ch17_2.cpp  **
    //**********************

    #include<iostream.h>

    class Furniture
    {
     public:
      Furniture(){}
      void SetWeight(int i){ weight =i; }
      int GetWeight(){ return weight; }
     protected:
      int weight;
    };

    class Bed :public Furniture
    {
     public:
      Bed(){}
      void Sleep(){ cout <<"Sleeping...//n"; }
    };

    class Sofa :public Furniture
    {
     public:
      Sofa(){}
      void WatchTV(){ cout <<"Watching TV.//n"; }
    };

    class SleeperSofa :public Bed, public Sofa
    {
     public:
      SleeperSofa() :Sofa(), Bed(){}
      void FoldOut(){ cout <<"Fold out the sofa.//n"; }
    };

    void main()
    {
     SleeperSofa ss;
     ss.SetWeight(20); //编译出错!模糊的SetWeight成员
     Furniture* pF;
     pF =(Furniture*)&ss; //编译出错!模糊的Furniture*
     cout <<pF->GetWeight() <<endl;
    }
因为SleeperSofa不是直接继承Furniture,而是Bed和Sofa各自继承Furniture,所以完整的SleeperSofa对象内存布局如图17-3所示。

图17-3 完整SleeperSofa对象内存布局

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


图17-4 SleeperSofa的实际继承关系

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

    #include<iostream.h>

    class Furniture
    {
     public:
      Furniture(){}
      void SetWeight(int i){ weight =i; }
      int GetWeight(){ return weight; }
     protected:
      int weight;
    };

    class Bed :virtual public Furniture
    {
     public:
      Bed(){}
      void Sleep(){ cout <<"Sleeping...//n"; }
    };

    class Sofa :virtual public Furniture
    {
     public:
      Sofa(){}
      void WatchTV(){ cout <<"Watching TV.//n"; }
    };

    class SleeperSofa :public Bed, public Sofa
    {
     public:
      SleeperSofa() :Sofa(), Bed(){}
      void FoldOut(){ cout <<"Fold out the sofa.//n"; }
    };

    void main()
    {
     SleeperSofa ss;
     ss.SetWeight(20);
     cout <<ss.GetWeight() <<endl;
    }
 运行结果为:
    20
  在Bed和Sofa继承Furniture中加上virtual关键字,这相当于说, “如果还没有Furniture类,则加入一个Furniture拷贝,否则就用有的那一个”。此时一个Sleepersofa在内存中的布局见图17-5。


图17-5 虚拟继承的SleepeiSofa内存布局

在虚拟继承的情况下,应用程序main()中引用GetWeight()不再模糊,我们得到了真正的图17-2所示的继承关系。
  ->虚拟继承的虚拟和虚拟函数的虚拟没有任何关系。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ky53900/archive/2009/10/15/4675595.aspx