Function Pointers from multiple classes
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.
Take a look at these two
http://www.boost.org/libs/bind/bind.html
http://www.boost.org/doc/html/function.html
Also note
http://www.boost.org/libs/bind/bind.html#with_boost_function
http://www.boost.org/libs/bind/bind.html
http://www.boost.org/doc/html/function.html
Also note
http://www.boost.org/libs/bind/bind.html#with_boost_function
boost::any can do what you want:
You might want to consider whether this is really the best way to do what you need or not though.
Enigma
#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
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.
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.
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:
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.
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.
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();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement