创建一个模板类的std :: vector? [英] creating a std::vector of a templated class?

查看:234
本文介绍了创建一个模板类的std :: vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个使用模板参数的类。我需要一个数组的这些。我怎么能这样做(没有boost)。
ex:

I currently have a class that uses template arguments. I need an array of these. How could I do this (without boost). ex:

template <typename RET, typename T>
class AguiTimedEvent {
    RET (*onEvent)(T arg);
    double timeStamp;
public:
    RET call(T arg);
    bool expired();
    AguiTimedEvent();
    AguiTimedEvent(RET (*Timefunc)(T arg), double timeSec);
};

,我需要类似:

AguiTimedEvent t<int, int>(func, 5.0);
v.push_back(t);
...
v[0].call();

我实际上不需要返回值,但我只是让它更灵活。如果返回值导致一个问题,我可以将其限制为void函数,但不可思议的是,arg需要模板化。我能做什么?感谢

I don't actually need the return value, but I just have it to make it more flexible. If the return value causes an issue I could limit it to void functions, but defiantly the arg needs to be templated. What can I do? Thanks

*我需要向量处理任何类型,我需要一个数组,其中数组可以分派XY的事件,而不只是int int

*I need the vector to handle any type, I need an array, where the array can dispatch events of X Y, not just int int

推荐答案

std::vector<AguiTimedEvent<int, int> > v;

如果您需要存储不同 AguiTimedEvent 具有不同模板参数的类型,您需要创建一个基类(例如 AguiTimedEventBase )并将指针存储在向量

If you need to store objects of different AguiTimedEvent types that have different template arguments, you need to create a base class (e.g., AguiTimedEventBase) and store pointers in the vector.

这篇关于创建一个模板类的std :: vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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