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_calla templated constructor using variadic templates like this:you can then derive from
common_callwith any template argument (e.g.base3) and call whatever constructor that class has defined