Sign in to follow this  

Function objects, mem_fun, boost::function etc.

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

Hi, I'm having some trouble understanding/using mem_fun and bind1st to pass a member function into a function that requires a function pointer as an argument. I have a constructor that looks like this:
ConsoleCommand(const string& command,
		CommandReturn (*handler)(Console*, const vector<string>& args),
		const string& usageText = string());
Now, if I create a "ConsoleCommand" using a static method for the second arg, it works fine. I have another class which has a function that looks like this:
CommandReturn Game::showFPS(Console*, const vector<string>& args);
I want to pass this as the 2nd parameter to the ConsoleCommand constructor, but bind the Game 'this' pointer as the 'this' instance of the showFPS function. I thought that this is what I needed to do:
ConsoleCommand(someConsole, bind1st(mem_fun(&Game::showFPS), this))
But it gives me a compiler error, I've also had a play with boost::function and boost::bind but I couldn't figure out a combination that worked. Can someone point me in the right direction? and explanation of how this stuff works behind the scenes would be cool. Thanks Luke.

Share this post


Link to post
Share on other sites
You can't. There's no way to construct an object that matches the signature of a function pointer (without stepping outside the bounds of the C++ Standard). What you can do is use a function object as your parameter, either as a template parameter (optionally with some concept checks):

template < typename Functor >
ConsoleCommand(const string & command, Functor handler, const string & usageText = string());
//..
ConsoleCommand command1("command1", &free_function);
ConsoleCommand command2("command2", std::bind1st(std::mem_fun(&Game::showFPS), this));
ConsoleCommand command3("command3", boost::bind(&Game::showFPS, this));


or, using boost::function (preferred syntax - doesn't work on all compilers) and boost::bind:

ConsoleCommand(const string & command, boost::function< CommandReturn (const vector< string > & ) > handler, const string & usageText = string());
//...
ConsoleCommand command1("command1", boost::bind(&Game::showFPS, this));


or, using boost::function (portable syntax) and boost::bind:

ConsoleCommand(const string & command, boost::function< CommandReturn, const vector< string > & > handler, const string & usageText = string());
//...
ConsoleCommand command1("command1", boost::bind(&Game::showFPS, this));


As always, add typedefs for readability.

Σnigma

Share this post


Link to post
Share on other sites

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