http://blog.163.com/kmustchenb@126/blog/static/11090576520108303581198/
C++虚继承可以防止多重继承产生的二义性问题。
虚继承,就是在被继承的类前面加上virtual关键字,这时被继承的类称为虚基类,如下面代码中的base类。虚继承在多重继承的时可以防止二义性。
以上的代码如果用到了base中的某个成员变量就不会产生二义性。和#progma once在头文件中的作用类似。请看下面的例子:
上面程序是不能通过编译的,编译器输出的错误信息如下:
…: error C2385: 'GrandChild::p' is ambiguous
…: warning C4385: could be the 'p' in base 'Parent' of base 'Child1' of class 'GrandChild'
…: warning C4385: or the 'p' in base 'Parent' of base 'Child2' of class 'GrandChild'
正如编译器告诉我们的那样,GrandChild::p是模棱两可,它被Child1继承了即Child1中包含了一个Parent subobject,也被Child2继承了即Child2中也包含了一个Parent suboject,然后GrandChild又同时继承了Child1和Child2,根据“derived class中要保持base class的完整原样性原则”,因此GrandChild包含了两个ParentObject。所以当pGC->p时,编译器根本无法确定是调用Child1::p还是Child2::p,由此边产生了模棱两可的情形。怎么解决这样的问题呢?答案就是用虚继承或者叫虚基类的方式。
在上面的示例程序中做如下改动:
class Child1 : public Parent
class Child2 : public Parent
GrandChild的定义维持不变:
class GrandChild : public Child1, public Child2
做了上述改动后,即增加了两个 virtual关键字,再编译就不会出现ambiguous之类的错误了。这是因为加上了virtual关键字后,就可以保证Parent suboject在GrandChild中只存在一份,从而消除了ambiguity。上面修改后的程序运行结果是13,这说明Child2类的那个p起了作用,如果GrandChild的定义写成:
那么运行结果将是12。
上面的试验结果表面,在多重继承的时候,如果父类中有同名的成员变量(类似这篇文章中谈及的例子),为了防止二义性,一般要采用虚继承的方式,并且最右边的基类中的那个成员变量会出现在派生类对象中。