简体   繁体   中英

Invoking Grandparent constructor with templated inheritance

I've chosen to use templated inheritance in order to avoid muliple and virtual inheritance. My goal is to make various children (4 or 5 generations or inheritance that I don't control) have a common function call regardless of what they derive.

My solution is inserting a template inheritance as so:

template <typename BASE>
class common_call : public BASE {
public:
    void foo() { /*implementation independent of base*/ }
};

class child1 : public common_call <base1> {};
class child2 : public common_call <base2> {};

This has the problem of invoking the constructor of base. Classes base1 and base2 (not written by me) have different constructors that I must invoke in the initialization list. The common_call template knows nothing about these constructors, but the child classes do as they currently inherit directly.

Is there any way for me to do this:

class child3 : public common_call<base3>{
public:
    child3(param1, param2) : base3(param2) {/*do things here*/}
};

I'm trying to avoid making partial template specializations for each type of base if possible.

If you give common_call a templated constructor using variadic templates like this:

template <typename BASE>
class common_call : public BASE 
{
public:
    // C++11 variadic templates to define whole range of constructors
    template<typename... Args>
    common_call(Args&&... args)
    :
        BASE(std::forward<Args>(args)...)
    {}

    void foo() { /*implementation independent of base*/ }
};

you can then derive from common_call with any template argument (eg base3 ) and call whatever constructor that class has defined

class child3
:
    public common_call<base3>
{
public:
    child3(Type1 param1, Type2 param2)
    :
        common_call(param2), // call constructor overload with 1 parameter of Type2
        t1_(param1)          // initialize t1_ member
    {}

private:
    Type1 t1_;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM