What is multiple re-inheritance?

前端 未结 2 1131
有刺的猬
有刺的猬 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.

    0 讨论(0)
  • 2021-02-13 08:07

    I’m adding to the accepted answer. It states a derived class cannot access a direct base class if the derived class also indirectly inherits base. Its solution makes the base class indirect by wrapping it with a template whose second type argument is tag. This ensures base is indirect to the derived class provided the derived class extends the wrapped base with a unique tag. The below example will use a non-type tag.

    If the diamond-like problem were generalized to contain more generations in the form of:

    • ith class inherits from base and (i – 1)th,
    • (i – 1)th inherits from base and (i – 2)th,
    • …, and
    • 2nd inherits from base,

    then it's an improvised container, where each element is stored in each uniquely tagged base. In that case, tag-making should be automated. One way is to consolidate all derived classes via a non-type template. Its non-type parameter N can specify the number of recursive inheritance iterations. By making tag a non-type parameter, the value of the parameter determining the number of subclasses can be uniquely related to that of the one tagging each subobject type. For example, tag = 10 corresponds to N = 10, which refers to the 10th generation on the hierarchy:

    // disambiguated_wrapper.h

    struct int_wrapper {
        int num;
    };
    
    template < typename base, unsigned int tag >
    struct disambiguated_wrapper : base {
        using base::base;
    };
    

    // improvised_container.h

    #include "disambiguated_wrapper.h"
    
    template <unsigned int N>
    struct improvised_container : 
        protected disambiguated_wrapper<int_wrapper, N>, 
        protected improvised_container<N - 1> {
    
        unsigned int size() const { return N; }
    
        int& at(const unsigned int index) {
            if (index >= N) throw "out of range";
            else return (index == N - 1) ?
                this->disambiguated_wrapper<int_wrapper, N>::num :
                this->helper(index);
        }
    protected:
        int& helper(const unsigned int index) {
            return (index == N - 1) ?
                this->disambiguated_wrapper<int_wrapper, N>::num :
                this->improvised_container<N - 1>::helper(index);
        }
    };
    #include "specializations.h"
    

    // specializations.h

    template <>
    struct improvised_container<0> {
        improvised_container() = delete;
    }; // ^ prohibits 0-length container
    
    template <>
    struct improvised_container<1> : 
        protected disambiguated_wrapper<int_wrapper, 1> {
    
        unsigned int size() const { return 1; }
    
        int& at(const unsigned int index) {
            if (index != 0) throw "out of range";
            else return this->disambiguated_wrapper<int_wrapper, 1>::num;
        }
    protected:
        int& helper(const unsigned int index) {
            if (index != 0) throw "out of range";
            else return this->disambiguated_wrapper<int_wrapper, 1>::num;
        }
    };
    

    // main.cpp

    #include "improvised_container.h"
    #include <iostream>
    int main() {
        improvised_container<10> my_container;
        for (unsigned int i = 0; i < my_container.size(); ++i) {
            my_container.at(i) = i;
            std::cout << my_container.at(i) << ",";
        }   // ^ Output: "0,1,2,3,4,5,6,7,8,9,"
    }
    

    Element access at cannot decrement the index to recursively call itself, because index isn’t a compile-time constant. But N is. So, at calls helper, which recursively calls the (i – 1)th version of itself in the (i – 1)th subobject, decrementing N until it equals index – 1, with each call moving one scope deeper, and finally returning the element of the target scope. It checks against index – 1 and not index, because the 0th improvised_container specialization's ctor is deleted. at compensates for the off-by-one.

    improvised_container uses protected inheritance to prevent client code from accessing the at and size methods of its base subobjects. The subobject’s size is less than the enclosing object’s.

    This works in g++ 4.8. The inheriting constructor using base::base causes errors in VC12, but it can be omitted because the element type is int.

    0 讨论(0)
提交回复
热议问题