Hi people. :)

I'm making a function callback system for a console, so I would dynamically be able to send a function pointer to my console class, and then it would be callable from the console.

Well, it works - but only with functions that are not member of a class.
I looked further into member function pointers, and I can't seem to figure a way to dynamically using them.

By dynamically, I mean any class. It seems to me, that I have to know the class name to make a typedef for the pointer.

And I wouldn't like to ruin my ability to have regular (non-member) function pointers too.


My typedef looks like this, now:

typedef void (*ConsoleFunction)(const std::string &);

And my function to add a pointer to the console is:

void Console::addItem(const std::string &name, void *point)
{
	ConsoleItem i;
	i.name = name;
	i.function = (ConsoleFunction) point;

	itemList.push_back(i);
}

The itemList is a simple STL list of ConsoleItem, and ConsoleItem is a struct containing name, pointer and a few other irrelevant variables.

So I'm wondering, how would I implement, so that I can use member functions with addItem aswell?

The classes will mostly be instanced, I don't know if it matters if its static or not, but if it matters, then the instanced class member pointers is the most important.

Hope to hear some advice (:

Thank you! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.