I would love create javascript’s setTimeout and setInterval like functionality for c++ (without boost). What I would like to achieve: A base class which could call its sub-classed member variables at a repeated or after a single delay.
I have an update function already and time elapsed functionality. I have also found how to pass a member function pointer to the base class and trigger that function using:
class BaseClass {
public:
template <class object>
void triggerNow(object *obj, void (object::*func)()) {
((obj)->*(func))();
}
}
class SubClass : public BaseClass {
public:
void update() {
triggerNow(this, &SubClass::worked)
}
void worked() {
cout << "worked!";
}
}
The problem I currently face is how to store object *obj and void (object::*func)() in a vector (or other container). I am only just figuring out templates…
How can I store the two templated parameters of triggerNow in a vector? Once I can figure this out, I can create my setTimeout and setInterval!
In order to store object* and object::*func in a vector you can do it like this:
Now you can store them as Callable’s in a std::vector, because the templated version is derived from Callable. If you store pointers in a std::vector<> though you need to remember to delete them at some point.
So will get something like this: