How to connect arbitrary memeber function in boost::signals?
Hi, all. I'm working in a project that need loosely coupling. I find boost::signals is perfect for that, save one exception. My question is that I want to connect arbitrary member function to signal. How to do it? Thanks in advanc. Yujiang Wang Feb. 15th, 2003
On Saturday 15 February 2003 09:48 am, "yjwang_bj wrote:
Hi, all.
I'm working in a project that need loosely coupling. I find boost::signals is perfect for that, save one exception.
My question is that I want to connect arbitrary member function to signal. How to do it?
Use the Bind or Lambda library. An example:
class X {
public:
void foo(int);
};
X* x;
boost::signal
Use the Bind or Lambda library. An example:
class X { public: void foo(int); };
X* x; boost::signal
sig; // So the signal will call x->foo(y) when sig(y) is called: sig.connect(boost::bind(&X::foo, x, _1));
Doug
It works but with the unusual C++ syntax. In libsigc++, I can do like this: sig1.connect(slot(x, &X::foo)); Hope boost has something like this though the boost's way is more powerful. Thanks a lot, doug. Yujiang Wang
On Tue, 18 Feb 2003, yjwang_bj
// So the signal will call x->foo(y) when sig(y) is called: sig.connect(boost::bind(&X::foo, x, _1));
Doug
It works but with the unusual C++ syntax. In libsigc++, I can do like this: sig1.connect(slot(x, &X::foo));
But you can't do this: sig.connect(boost::bind(&X::foo, x, _3, y, boost::bind(f, _2, _1)));
Hope boost has something like this though the boost's way is more powerful.
Boost does not have what you are looking for. The bind syntax may be a little unusual, but for that slight oddity you get a syntax that is arbitrarily extensible. Perhaps I have been (un?)lucky, but I've found that in any project that needs the simple form (e.g., slot(x, &X::foo)), there is inevitably some place in the same project that needs a more advanced form. Once someone has grasped boost::bind to understand the advanced uses, the simple uses of boost::bind are trivial; why learn another primitive? Doug
participants (3)
-
Douglas Gregor
-
Douglas Paul Gregor
-
yjwang_bj <yjwang_bj@yahoo.com>