Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Class inheriting from template class without knowing the type?

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.

like image 910
Ælex Avatar asked Jul 12 '11 21:07

Ælex


People also ask

Can a template class inherit from a non template class?

Deriving from a non-template base class There is no requirement that your base class be a template. It is quite possible to have a template class inherit from a 'normal' class. This mechanism is recommended if your template class has a lot of non-template attributes and operations.

Can I inherit from template class?

Inherited Member Functions of Class Templates are not Available. Let's start simple. I implemented a class Base and Derived. Derived is public derived from Base and can, therefore, be used in its method callBase (line 2) the method func from class Base.

What is the difference between typename and class in template?

There is no semantic difference between class and typename in a template-parameter. typename however is possible in another context when using templates - to hint at the compiler that you are referring to a dependent type. §14.6.


2 Answers

Make them template classes:

template <typename P>
class Greedy : public Policy<P>
{
    // now you know
};
like image 75
GManNickG Avatar answered Sep 22 '22 01:09

GManNickG


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.

like image 33
Kerrek SB Avatar answered Sep 22 '22 01:09

Kerrek SB