What is multiple re-inheritance?

前端 未结 2 1134
有刺的猬
有刺的猬 2021-02-13 07:11

I refer to the following as “multiple re-inheritance”:

  • inheriting a class once directly and one or more times indirectly by inheriting one or more of its descendan
2条回答
  •  感情败类
    2021-02-13 08:06

    The common term for this is the diamond pattern (or diamond problem).

    It is not an error per se, but as noted in the comments here, any attempt to access a direct base that is reduplicated elsewhere in the hierarchy will result in an ambiguity error.

    One workaround would be to make the base indirect. The new inheriting constructors feature in C++11 allows perfect wrappers:

    template< typename base, typename tag >
    struct disambiguated_base : base
        { using base::base; };
    

    Given an unused tag type, this generates a new class derived from, and functionally identical to, the given base. The tag type may be an incomplete class denoted by an elaborated-type-specifier:

    struct GrandChild : Parent,
        disambiguated_base< GrandParent, class grandchild_grandparent_tag > {
    
        typedef disambiguated_base< GrandParent, grandchild_grandparent_tag >
            my_direct_grandparent;
    
        int num;
    };
    

    Now GrandChild can use my_direct_grandparent:: to disambiguate member accesses.

提交回复
热议问题