Skip to main content
gamedev.net
🔒 Locked

boost::function, boost::bind and member function pointers

Started by IFooBar Dec 30, 2003 at 5:43 AM 4 replies 350+ views
Original Post
IFooBar
IFooBar
Ok this is the problem: I have a Window class that has function pointers to a function that returns void and accepts a Window pointer as a parameter. They are defined like so class Window { ... typedef boost::function1<void, Window*> MessaheHandler_pfn; ... MessageHandler_t OnResume; }; Now I can assign normal functions to it without problems, but I want to be able to assign member functions of other classes to it. I *thought* that this was possible with boost::bind, but it isnt. I tried doing this:
class SomeClass
{
    Window m_window;

public:
    SomeClass()
    {
        m_window.OnResume = boost::bind( &SomeClass::SomeFunction, this, &m_window );
    }
    virtual void SomeFunction( Window* )
    {
            
    }
};
Now obviously that wont work because bind produces a function with 2 parameters, and 'OnResume' is a function that takes one parameter. How can I achieve this? is there some other boost library that will allow for this that I've overlooked or something? This does however work if in 'SomeClass's constructor I do: m_window.OnResume = boost::bind(&SomeClass::SomeFunction, this ); But that just allows for compilation....it's still painfully wrong

| C++ Debug Kit :: GameDev Kit :: DirectX Tutorials :: 2D DX Engine | TripleBuffer Software |
| Plug-in Manager :: System Information Class :: D3D9 Hardware Enum | DevMaster :: FlipCode |

[edited by - ifoobar on December 30, 2003 8:05:11 AM]
[size=2]aliak.net
IFooBar
IFooBar
Ay, it is.

Man, I can''t think of a whitty reply (which this situation is in dire need of)

Though I did read the docs a bunch of times, can''t for the life of me see why I''d miss that.

| C++ Debug Kit :: GameDev Kit :: DirectX Tutorials :: 2D DX Engine | TripleBuffer Software |
| Plug-in Manager :: System Information Class :: D3D9 Hardware Enum | DevMaster :: FlipCode |

[size=2]aliak.net
antareus
antareus
Just so you know, if a function takes two parameters:

boost::bind(Class::memberFunction, instance, _1, _2);

That took me awhile to figure out reading the docs. If you want to partially evaluate the function (e.g. fill in arguments and store them inside the functor) you can substitute actual args in for _1, _2 and they''ll be remembered.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

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

Sign in to reply to this topic.