Boost::bind member function and save to function pointer

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
Can it be done? Thanks in advance.

funcPointer = boost::bind(&ChatClient::memberReceiver, this, _1);
Hey, I can finally be of some use, this is cool!
that does not work. what boost::bind is returning in that case is a
boost::function object, like this:
boost::function

On Thu, Apr 14, 2011 at 1:27 AM, Littlefield, Tyler
This seems to be common misconception. bind does _not_ return a boost::function object. It never did, it never will. Never. It returns some type that is callable, thus you can assign it to, construct a boost::function object with the right signature from it.
HTH, and happy hacking!

Rather than re-inventing the wheel, you might try boost::signal or fastdelegates: http://www.codeproject.com/KB/cpp/fd.aspx http://www.codeproject.com/KB/cpp/FastDelegate.aspx -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
participants (5)
-
David McCallum
-
Littlefield, Tyler
-
Raymond Burkholder
-
Steven Watanabe
-
Thomas Heller