Jump to content
  • Advertisement
Sign in to follow this  
ZealousEngine

How to store a function copy/pointer w/VARIABLE arguments?

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

Heres what I want to do...
//I start with a function...
void foo( int val ) { //do something with val }

//now I want to store/wrap the function somehow, so it can be called later
boost::function( void() ) func;

//however, unless I HARD CODE the 'val' argument, things wont compile...

//works fine
func = boost::bind( &foo, 25 );
func(); //is the same as foo(25);

//does NOT work :(
func = boost::bind( &foo, _1 );
func(44); //SHOULD be the same as foo(44), but doesnt work
func(78); //SHOULD be the same as foo(78), but doesnt work
Can anyone tell me why this is? Or more importantly, how I can make it 'work'? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You said that func does not have arguments:
boost::function( void() ) func;

Then, you tried to call it with an integer argument:
func(44);

So, you must have been wrong on one of these accounts.

Share this post


Link to post
Share on other sites
Oh? So my problem was with how I declared func? I dont have much experience with boost::function, how SHOULD I have declared it?

like... boost::function<void()>(int) func; ?

Share this post


Link to post
Share on other sites
Hmm well were one step closer, however I still get an error when trying to bind...


boost::function< void(), int > func;

func = boost::bind( &foo, _1 );


doesnt work :(

Share this post


Link to post
Share on other sites
No reason. If there is another way to do what im trying to do, im all for it. I just always try and use boost because I use it for lots of other things (threads, arrays, ect..)

Share this post


Link to post
Share on other sites
If you have no reason to use it, why not use one of the two types I suggested in my previous response?

Share this post


Link to post
Share on other sites
Because that would be too easy!

Hah ok I see now, I just assumed you HAD to use void() in order to speicivy a void return. However now that I look at it, - boost::function<void(int)> is such a more logical syntax.

Thanks! Works great!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!