Using C++ base class constructors?

后端 未结 5 1990
挽巷
挽巷 2020-12-23 13:36

While working with templates I ran into a need to make a base class constructors accessible from inherited classes for object creation to decrease copy/paste operations. I w

5条回答
  •  爱一瞬间的悲伤
    2020-12-23 14:06

    Yes, Since C++11:

    struct B2 {
        B2(int = 13, int = 42);
    };
    struct D2 : B2 {
        using B2::B2;
    // The set of inherited constructors is
    // 1. B2(const B2&)
    // 2. B2(B2&&)
    // 3. B2(int = 13, int = 42)
    // 4. B2(int = 13)
    // 5. B2()
    
    // D2 has the following constructors:
    // 1. D2()
    // 2. D2(const D2&)
    // 3. D2(D2&&)
    // 4. D2(int, int) <- inherited
    // 5. D2(int) <- inherited
    };
    

    For additional information see http://en.cppreference.com/w/cpp/language/using_declaration

提交回复
热议问题