Search code examples
c++c++11templatesinheritancevariadic-templates

Inherit from classes passed to constructor as a template parameters an inherit from them


I want to have something like that:

template<class... Args>
class MyClass : public Args
{
    MyClass<class... Args>(/*some parameters*/)
    { }
}

// ana then:
MyClass<Base1, Base2, Base3>(/*some arguments*/)

That i want to dynamic_cast to Base1 or etc. and to use his methods.

I know that this code will not work, but do you have any ideas how to do it?


Solution

  • This works fine:

    struct Base1
    {};
    struct Base2
    {};
    struct Base3
    {};
    
    template<class... Args>
    struct MyClass : public Args...
    {};
    
    MyClass<Base1, Base2, Base3> mc;
    

    There is no need for the <> in your constructor, you are missing "..." after public Args and you are missing a ";" at the end of your class definition.