You need a function pointer:
void* (*OnJoinFn)(char*, char*);
In your class Irc
class Irc { public: OnJoinFn onJoin; };
This can be assigned as you do above:
int main() { Irc irc(stuff); irc.onJoin = join; }
But I am wondering if you are just learning C ++, do you really need a function pointer? function pointers are legally legal and valid, but an unusual object, and I usually expect to use some other mechanism. In the beginning, I would suggest looking at abstract base classes:
class IIrc { public: virtual void* OnJoin(const char*, const char*) = 0;
I allowed to introduce const correctness in OnJoin .
You should also consider returning void* , which bypasses most security mechanisms like C ++, but a pointer to the actual object or other interface.
Finally, using new (and delete , which is missing here, resulting in a memory leak) is bad practice. Instead, prefer to allocate things on the stack or, if you really need dynamic allocation, use a smart pointer.
source share