I am trying to implement the following code:
template<class ManagerBase>
class ManagerA : public ManagerBase {};
template<class ManagerBase>
class ManagerB : public ManagerBase {};
class Base {
public:
template<typename ManagerBase>
virtual ManagerBase* CreateManager() const = 0;
};
class A : public Base{
public:
template<typename ManagerBase>
virtual ManagerBase* CreateManager() const { return new ManagerA<ManagerBase>; }
};
class B : public Base{
public:
template<typename ManagerBase>
virtual ManagerBase* CreateManager() const { return new ManagerB<ManagerBase>; }
};
class SomeManagerBase {};
int main() {
Base* pPolymorphic = new A;
SomeManagerBase* pBase = pPolymorphic->CreateManager<SomeManagerBase>();
}
But since C++ doesn't seem to allow mixing virtual functions with templates, how can I achieve that? I've already thought about having a switch
on the base class instead of a pure function so it would call the right function from the base itself, but this doesn't seem a good approach because each time I create a new class that derives from Base
I'll have to change this function, so I was expecting someone would have a better idea of what can be done here.