C++ Class inheriting from template class without knowing the type?
Solution 1
Make them template classes:
template <typename P>
class Greedy : public Policy<P>
{
// now you know
};
Solution 2
You can certainly do it (see GMan's answer for the correct parametrization of the derived type), but bear in mind that you will get entirely independent class hierarchies for every type P
. You are not magically creating a super class that has an arbitrary number of member types.
Consider templates as a code generation tool. They do not create a "type-generic type", rather they create lots of parallel instances of concrete, static types at compile time following a generic pattern.
If you truly want one single common base type, perhaps you can make the type of state
polymorphic:
class Policy // no template
{
StateBase * state;
public:
Policy(StateBase * s) : state(s) { }
};
Then all your derived classes can access the states' common interface.
Related videos on Youtube
![Ælex](https://i.stack.imgur.com/kbSI9.jpg?s=256&g=1)
Ælex
I'm an ML/AI researcher who sold his soul to the industry. I love working on RL, ML, CV using Python (PyTorch, Keras, TF, etc). Used to work on C++/ROS/OpenCV for Robotics. I'm not looking for a job, but I'm definitely interested in Startups.
Updated on June 01, 2022Comments
-
Ælex about 2 years
I am designing a template class Policy which needs to be able to handle pointers to other classes.
template <class P> class Policy { private: const P *state; public: Policy (P const* s) : state(s){}; };
This works fine. Now I want to inherit from the above template class and create new subclasses:
class Greedy : public Policy<???> { public: template <typename P> Greedy (P const* s) : Policy(s) {}: }; class Explora : public Policy<???> { public: template <typename P> Explora (P const* s) : Policy(s) {}: };
Problem is that when defining those classes I do not know what type they will be using for the base template class. Is this even possible to do ? I want the type obtained from the inherited class constructor (probably templated), and then pass it to the base class construtor. Can I do that ? If yes, how ? typedefining enums ? I have seen this question but it doesn't in my opinion really answer the question.
-
Ælex almost 13 yearsSo is this bad ? Will it bloat up the executable ?
-
Ælex almost 13 yearsThank you, I was under the impression that a template class could not inherit from a template class, or is it only if the base class has a virtual ?
-
Kerrek SB almost 13 yearsIt depends entirely on what you want to achieve. Templates and inheritance are sort of orthogonal concepts; templates are type-generic, work "in parallel" and don't see each other, while inheritance is type-fixed and "goes down". Use templates if you have a repeating pattern, and inheritance if you have common interfaces.
-
Ælex almost 13 yearsWell in this case I want a combination, of a baseclass which can work which will be generic (work with many types), yet be inherited from other classes. Is it bad practise to mix those two ?
-
GManNickG almost 13 years@Alex: I template class is just a normal class, once you give it template arguments.
-
GManNickG almost 13 years@Alex: Get it working in the cleanest fashion possible, then worry about performance.