Writing the code like this newEvent((*Event1)(), "Event1");
is wrong because you are dereferencing a function name.
Writing it as newEvent(Event1(), "Event1");
would mean that you are trying to use the return value of a void function as a function pointer.
Saying newEvent(Event1, "Event1");
is only a little better, since you'll be trying to pass a "pointer to member function" as a "pointer to function". Since a member function needs an instance to run, this won't work either.
I don't know what your larger architecture looks like, but a more general solution would be to use std::function
instead of simple function pointers:
struct Event
{
std::function<void()> run; // requires <functional>
bool triggered;
std::string name;
};
Now, you can pass in ordinary function pointers, pointer-to-members, lambdas, functors, etc. as your callback.
Here's one way to change the rest of the code to make it work (note: it is not the only way; but it's the easiest
way IMO to have generality and flexible design. Full source code here.)
Change the first line of newEvent
to this:
template <typename F>
void newEvent(F onRun, std::string eventName)
{...}
And call it like this:
newEvent([=](){Event1();}, "Event1");
// ^^^^^^^^^^^^^^^^
// This is a lambda
These are C++11 features. If you are unfamiliar or uncomfortable or unable to use C++11 features for some reason, I'd suggest using the old virtual base class interface way, with a specific pure virtual method as a mouse even callback, and passing around pointers to objects instead of pointers to member functions.
You can pass around and later call pointer to member functions, but you'd need a pointer to an instance of that specific class to call it on too. I think the design will quickly get complicated and is not worth it, but you can explore that route if you are so inclined.