由类forbit_mi,类one和类two以及类my组成了类继承的层次结构。在该结构中,类my的对象将包含两个类forbit_mi的子对象。由于类forbit_mi是派生类my两条继承路径上的一个公共基类,那么这个公共基类将在派生类的对象中产生多个基类子对象。如果要想使这个公共基类在派生类中只产生一个基类子对象,则必须将这个基类设定为虚基类。**虚基类的真正目的是为了解决二义性问题!

解决方案 »

  1.   

    先多谢各位回复!
    to : hgw111(hg) 
    我也怀疑能否做到to : yu_hl(卖柴火的小男孩)
         edrftgyh(老胡) 
         lshadow(光影)
         jlqidan(jlqidan)
    你们可能误会了我得意思。“虚基类可以解决只产生一个基类子对象”这一点我也非常清楚。我要得不是这一点,在编译时完全禁止多重继承。例如:
    定义:
    template<class T>
    class no_inherit{
    private:
       no_inherit();
       friend class T;
    };
    那么
    class A:public no_inherit<A>{
    };将不能被继承
    class B : A 这样的代码将出错。
    我要的就是类似的编译时刻的报错机制
      

  2.   

    to it_worker,run-time check can be easily achieved. i just reviewed my solution given in the c/c++ forum. i would think it only solves one aspect of your mi problem. the other aspect of the problem is: how to design a class that can not be multiple inherited (i.e. produce error at compile time if it is multiple inherited). this problem is interesting. thanks for bringing it up.