Boost logo

Boost Users :

Subject: [Boost-users] Boost::bind member function and save to function pointer
From: David McCallum (dmccallum9_at_[hidden])
Date: 2011-04-13 19:08:06


I'd like to implement an event-handling scheme that supports member
functions. For example, I would have a class that accepts a function
pointer, stores it, and calls that function when some event occurs.
I'd like to be able to store a pointer to a member function with
boost::bind():

void staticReceiver(std::string message)
{
    cout << message << endl;
}//staticReceiver

void ChatClient::memberReceiver(std::string message)
{
    cout << message << endl;
}//testReceiver

void ChatClient::testBind(void)
{
    void (*funcPointer)(std::string message);

    boost::bind(&ChatClient::memberReceiver, this, _1)("Bind works");

    funcPointer = staticReceiver;
    (*funcPointer) ("Function pointer works");

    //##### THIS LINE WILL NOT COMPILE #####
    funcPointer = boost::bind(&ChatClient::memberReceiver, this, _1);
}//testBind

However, I get the following compiler error:

g++ -DHAVE_CONFIG_H -I. -I./libdasyne/lowlevel -I/opt/boost/include
-D_REENTRANT -Wall -g -O2 -MT ChatClient.o -MD -MP -MF
.deps/ChatClient.Tpo -c -o ChatClient.o `test -f
'examples/ChatClient.cpp' || echo './'`examples/ChatClient.cpp
examples/ChatClient.cpp: In member function ‘void ChatClient::testBind()’:
examples/ChatClient.cpp:56: error: cannot convert
‘boost::_bi::bind_t<void, boost::_mfi::mf1<void, ChatClient,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
>, boost::_bi::list2<boost::_bi::value<ChatClient*>, boost::arg<1> >
>’ to ‘void (*)(std::string)’ in assignment
make: *** [ChatClient.o] Error 1

Can it be done? Thanks in advance.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net