Re: [Boost-users] shared_ptr and signals
Hello,
Perhaps nobody is aware of (perpahs some are) that this simple
use case has introduced another intersting and, probably, very
useful scenario in which the Signals lib could be benificial.
That is:
Slot With Status.
Have a look at this example:
struct Slot
{
int sum;
Slot() : sum(0) {}
Slot(int i) : sum(i) {}
int operator()(int i) { return sum += i; }
};
void test_signals()
{
typedef boost::signal
James C. Sutherland wrote:
Is it possible to use a shared_ptr object in a signal?
I want something like
struct A{ ... void operator()(){ ... } ... }; typedef boost::signal Signal; Signal mySignal;
boost::shared_ptr a( new A() ); mySignal.connect(a);
Wrap the shared_ptr in a function object
struct deref_call { void operator()() const { (*f)(); } boost::shared_ptr f; };
bind can also be used to do this, since shared_ptr can be passed as the "this" argument when binding member functions: boost::shared_ptr a( new A() ); mySignal.connect(boost::bind(&A::operator(), a)); ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
participants (1)
-
Max