C++ 类在不知道类型的情况下从模板类继承?

标签 c++ class templates inheritance

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

template <class P>
class Policy
{
  private:   
    const P *state;
  public:
    Policy (P const* s) : state(s){};
};

这很好用。 现在我想继承上面的模板类并创建新的子类:

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

问题是,在定义这些类时,我不知道它们将为基本模板类使用什么类型。这甚至有可能做到吗? 我想要从继承类构造函数(可能是模板化的)获得的类型,然后将它传递给基类构造函数。 我可以这样做吗?如果是,如何?类型定义枚举? 我看过this question但我认为它并不能真正回答问题。

最佳答案

使它们成为模板类:

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

关于C++ 类在不知道类型的情况下从模板类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6671446/

相关文章:

iphone - 确定 View 是否位于 UIPopOverController 内部

c# - AForge.Imaging.dll 上缺少新添加的类

c++ - llvm JIT addGlobalMapping,不能添加类成员函数,这是一个模板函数

c++ - unique_ptr(std::nullptr_t) 和模板

c++ - 字符、指针、强制转换和字符串问题

php - 从 C++ 运行 PHP 脚本

c++ - 如何在模板实例化时故意导致编译时错误

java - 存储嵌套列表

c++ - 模板函数的部分 typedef (`using` )

C++ char* x = fgets() 不工作