我正在设计一个模板类 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/