C ++类从模板类继承而不知道类型? [英] C++ Class inheriting from template class without knowing the type?

查看:145
本文介绍了C ++类从模板类继承而不知道类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个模板类Policy,需要能够处理指向其他类的指针。

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) {}:
};

问题是,当定义这些类时,我不知道他们将使用什么类型的基本模板类。这是可能吗?
我想从继承类构造函数(可能是模板)获得类型,然后将其传递给基类construtor。
我可以这样做吗?如果是,如何? typedefining枚举?
我看到了

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.

推荐答案

使它们成为模板类:

template <typename P>
class Greedy : public Policy<P>
{
    // now you know
};

这篇关于C ++类从模板类继承而不知道类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆