Skip to main content
GameDev.net gamedev.net
🔒 Locked

boost::bind on overloaded methods

Started by wild_pointer Jan 22, 2006 at 5:44 PM 5 replies 12.7k views
Original Post
wild_pointer
wild_pointer
Is there a way to let bind know which version of an overloaded method I'd like to use?

struct Foo {
  void Bar(int x);
  void Bar(float x);
};

// --

Foo* foo = new Foo();
boost::bind(&Foo::Bar, foo, _1); // 100+ errors!

[size=2]
cypherx
cypherx
I haven't used Boost bind, but have you tried specifying the function signature?

Would boost::bind(&Foo::Bar(int), foo, _1); work?

Also, what is "new Foo();"? Is that the same as "new Foo;"?

-Alex
dave_
dave_
The compiler cant tell which one you want to use, you have to specify the template parameters. HAve a look at the documentation for examples of how to do this. Theres an example in the section titled Using bind with function objects
Dorvo
Dorvo
Here's the direct link that dave_ was referring to.
Fruny
Fruny
When taking the address of an overloaded function, you specify which overload you are referring to by casting the pointer to the appropriate type:

boost::bind( static_cast<void (Foo::*)( int )>(&Foo::Bar) , foo, _1);boost::bind( static_cast<void (Foo::*)(float)>(&Foo::Bar) , foo, _1);


Breaking the process into two steps (take the pointer, pass it to bind) may make things clearer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
wild_pointer
wild_pointer
Quote:
Original post by dave_
The compiler cant tell which one you want to use, you have to specify the template parameters. HAve a look at the documentation for examples of how to do this. Theres an example in the section titled Using bind with function objects


I believe the template argument is only for specifying return type? Perhaps I've misunderstood.

[size=2]
wild_pointer
wild_pointer
Quote:
Original post by Fruny
When taking the address of an overloaded function, you specify which overload you are referring to by casting the pointer to the appropriate type:

boost::bind( static_cast<void (Foo::*)( int )>(&Foo::Bar) , foo, _1);boost::bind( static_cast<void (Foo::*)(float)>(&Foo::Bar) , foo, _1);


Breaking the process into two steps (take the pointer, pass it to bind) may make things clearer.


Thank you!
[size=2]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.