从意义上来看,一个SleeperSofa没有沙发和床两种重量, 如此的继承不是真实的现实世界描述。进一步分析可得,床和沙发都是家具的一种,凡家具都有重量,所以通过分解来考察其关系,如图1所示。
图1 床和沙发的分解
- //** Sample1.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组合继承,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)。
下面是虚拟继承的代码:
- //** Sample2.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);
- Furniture* pF;
- pF =(Furniture*)&ss;
- cout <<pF->GetWeight() <<endl;
- }
运行结果为:20
在Bed和Sofa继承Furniture中加上virtual关键字,这相当于说,“如果还没有Furniture类,则加入一个Furniture拷贝,否则就用有的那一个”。此时一个Sleepersofa在内存中的布局见图4:
图4 虚拟继承的SleepeiSofa内存布局
在虚拟继承的情况下,应用程序main()中引用GetWeight()不再模糊,我们得到了真正的图1所示的继承关系。
虚拟继承的虚拟和虚拟函数的虚拟没有任何关系。