B C虚继承A,D public继承 B C ,有A *a = new D,a->fun(),fun是虚函数,并且B C都重写了,怎么保证a调用的是B重写的虚函数。
#include <iostream>
using namespace std;class A
{
public:virtual void fun() { cout << "A::fun()." << endl; }
};class B :public virtual A
{
public:void fun() { cout << "B::fun()." << endl; }
};class C :public virtual A
{
public:void fun() { cout << "C::fun()." << endl; }
};class D :public B, public C
{
public:void fun() { cout << "D::fun()." << endl; }
};int main()
{A* a = new D;(dynamic_cast<B*>(a))->B::fun();return 0;
}
// 输出:B::fun().