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<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.
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<void (std::string)>; I believe is how you had your message receiving function declared. In which case, you will need to declare another variable for that. HTH, and happy hacking!
AMDG On 04/13/2011 04:27 PM, Littlefield, Tyler wrote:
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:
Well, technically, it returns an unspecified type that can be converted to such a boost::function.
boost::function<void (std::string)>; I believe is how you had your message receiving function declared. In which case, you will need to declare another variable for that.
In Christ, Steven Watanabe
On Thu, Apr 14, 2011 at 1:27 AM, Littlefield, Tyler <tyler@tysdomain.com> wrote:
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<void (std::string)>; I believe is how you had your message receiving function declared. In which case, you will need to declare another variable for that.
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!
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():
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