Consequences of changing inheritance to virtual?

后端 未结 4 1294
礼貌的吻别
礼貌的吻别 2020-12-31 15:50

I\'m working on a huge project that I didn\'t start. My task is to add some additional functionality to what already is there. I\'m in a situation where I have to use virtua

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 16:47

    According to the standard, virtual inheritance is exactly the same as non virtual one except that only one single instance of the virtualy inherited class exists in the derived object.

    So in nothing in original code had multiple inheritance on classes derived from Base, changing the inheritance of Base to be virtual should not change behaviour. But you must consult of build the class hierachy to be sure of it.


    Refs from n4096 draft:

    10.1 Multiple base classes [class.mi]
    ...
    4 A base class specifier that does not contain the keyword virtual, specifies a non-virtual base class. A base class specifier that contains the keyword virtual, specifies a virtual base class. For each distinct occurrence of a non-virtual base class in the class lattice of the most derived class, the most derived object (1.8) shall contain a corresponding distinct base class subobject of that type. For each distinct base class that is specified virtual, the most derived object shall contain a single base class subobject of that type.

    And except from the examples following that paragraph, I could find no other reference to virtual inheritance in [class.mi].

提交回复
热议问题