Sign in to follow this  

Function Pointers from multiple classes

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is this possible? I've been messing around with function pointers, and am stuck trying to find a way, if it is possible, to having a list of pointers for functions from different classes within the same map? The reason i'm doing this is to simplify the creation of a basic scripting language (using this in a case study at uni), and within a game console system, and within the console system it would be nice if the console commands could call functions within, say, the renderer class, the input class, so each of the core classes could have their own console commands without the need to write functions seperatly to wrap to those classes. Sorry if it doesnt sound very clear, made sense in my head.

Share this post


Link to post
Share on other sites
boost::any can do what you want:
#include <deque>
#include <iostream>
#include <boost/any.hpp>

class Test1
{

public:

void func()
{
std::cout << "Test1\n";
}

};

class Test2
{
public:

void func()
{
std::cout << "Test2\n";
}

};

typedef void (Test1::* test1MemberFunction)();
typedef void (Test2::* test2MemberFunction)();

int main()
{
std::deque<boost::any> funcs;
funcs.push_back(&Test1::func);
funcs.push_back(&Test2::func);
Test1 t1;
Test2 t2;
(t1.*(boost::any_cast<test1MemberFunction>(funcs[0])))();
(t2.*(boost::any_cast<test2MemberFunction>(funcs[1])))();
}


You might want to consider whether this is really the best way to do what you need or not though.

Enigma

Share this post


Link to post
Share on other sites
theoretically, no.

realistically, yes. But the functions all need to have the same signature. I point to the same article that I always use when member pointers come up. It's a really freaking great story about the very problem you describe, and a very useful library to use.

Share this post


Link to post
Share on other sites
Already looked there, I can make function pointers to class members, but I wanted to have members from other classes, I will demonstrate with some kind of psudo code:


class ObjectOne;
class ObjectTwo;
class ObjectThree;

ObjectOne g_cOne;
ObjectTwo g_cTwo;
ObjectThree g_cThree;

map<std::string, (Something to do multiple class pointers here, unsure how to)> funcPtr;

void makepointers()
{
funcPtr["One"] = &ObjectOne::Function;
funcPtr["Two"] = &ObjectTwo::Function;
funcPtr["Three"] = &ObjectThree::Function;
}





My main problem is storing pointers to functions from several classes, the usual way of storing a function pointer in a map wont work, and I would need to pass which class container (help me out with the correct terminology? the g_cOne, g_Two thing) when calling the function pointer.

I'm probably going to have to write all the functions I want to point to in one class, then wrap to the real functions I want to point to. I was hoping there was a slightly easier, shorter, and neater method of doing this, ah well.

Share this post


Link to post
Share on other sites
You talk a lot about classes but little about objects. I still think my links have the solution for you.

map<std::string, boost::function<void ()> > funcPtr;

void makepointers()
{
funcPtr["One"] = boost::bind(&ObjectOne::Function, &g_cOne);
funcPtr["Two"] = boost::bind(&ObjectTwo::Function, &g_cTwo);
funcPtr["Three"] = boost::bind(&ObjectThree::Function, &g_cThree;
}

//elsewhere:

funcPtr["One"](); //call g_cOne.Function();
funcPtr["Two"](); //call g_cTwo.Function();

Share this post


Link to post
Share on other sites

This topic is 4744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this