Jump to content
  • Advertisement
Sign in to follow this  
guvidu

Passing a string to a callback function

This topic is 3713 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

Hello Maybe you could help me with this. I have this function Function(void(COpenGL::*Callback)()); what i want to do is to pass to this function a string. so im thinking something like this string funcString = "COpenGL::NeededFunc"; std::stringstream ss; ss << funcString; void* value; ss >> value; Function(value); well of course this doesnt work - now comes my question - is there another way to what i need - or maybe this is the way but im doing it wrong. Can you help me? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Just modify you callback signature to include a string argument. In the simplest case:

#include <iostream>

typedef void CallbackFunction(const std::string &);

void Test(CallbackFunction *callback) {
callback("Hello World");
}

void MyCallback(const std::string &someString) {
std::cout << someString << std::endl;
}

int main() {
Test(&::MyCallback);
}


If your intention is to do a callback to a method (it seems so), just do the same for the method callback signature:

class COpenGL {
void (COpenGL::*callback)(const std::string &);

void myCallback1(const std::string &someString) {
std::cout << "MyCallback1: " << someString << std::endl;
}
void myCallback2(const std::string &someString) {
std::cout << "MyCallback2: " << someString << std::endl;
}

void test() {
this->callback = &COpenGL::myCallback1;
(this->*callback)("Hello World");
this->callback = &COpenGL::myCallback2;
(this->*callback)("Hello World");
}
};


Hope that helps!

Share this post


Link to post
Share on other sites
Thanks - i decided to do a stl map - and asociate a function to a string - its suite what i need

Share this post


Link to post
Share on other sites
Ah, so you wanted to call a function by name. Sorry, I totally misunderstood your intention.

Your map<> is the right way to go. C++ doesn't support reflection, so creating a name lookup yourself is the only possible solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!