创建一个模板类的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.