W
wenmang
Hi, all:
I am reading Herb Sutter's article:
http://www.cuj.com/documents/s=8840/cujexp0309sutter/
I have trouble to understand for following lines:
class Subject {
// ...
public:
virtual void Attach( function<void (Subject*)> o ) {
obs_.push_back(o); } --Line1
virtual void Detach( function<void (Subject*)> o ) { /* ... */ }
virtual void Notify() {
for( list<function<void (Subject*)> >::iterator i = obs_.begin();
i != obs_.end(); ++i )
(*i)( this );
}
private:
list<function<void (Subject*)> > obs_;
};
class ClockTimer : public Subject { // as before
// ...
void Tick() {
// ... timekeeping logic...
Notify(); // every so often, tick
}
};
class DigitalClock : /*...*/ public Observer { // still works
// ...
public:
DigitalClock( ClockTimer* t ) : timer_(t)
{ timer_->Attach( bind1st( mem_fun( &DigitalClock::OnUpdateOf ),
this ) ); } --Line2
~DigitalClock()
{ timer_->Detach( bind1st( mem_fun( &DigitalClock::OnUpdateOf ),
this ) ); }
void OnUpdateOf( Subject* timer ) { /* query timer and redraw */ }
private:
ClockTimer* timer_;
};
Here are my questions:
1. mem_fun() generates the unary functor, but bind1st() takes binary
function as its 1st argument, how does Line2 work?
2. Could you please provide stepwise explanation how Line2 being
expanded to fit member function signature of Attach(function<void
(Subject*)> )?
3. is "this" on Line2 a DigitalClock*? DigitalClocl* is not Subject*
for Attach(), isn't?
Thanks.
I am reading Herb Sutter's article:
http://www.cuj.com/documents/s=8840/cujexp0309sutter/
I have trouble to understand for following lines:
class Subject {
// ...
public:
virtual void Attach( function<void (Subject*)> o ) {
obs_.push_back(o); } --Line1
virtual void Detach( function<void (Subject*)> o ) { /* ... */ }
virtual void Notify() {
for( list<function<void (Subject*)> >::iterator i = obs_.begin();
i != obs_.end(); ++i )
(*i)( this );
}
private:
list<function<void (Subject*)> > obs_;
};
class ClockTimer : public Subject { // as before
// ...
void Tick() {
// ... timekeeping logic...
Notify(); // every so often, tick
}
};
class DigitalClock : /*...*/ public Observer { // still works
// ...
public:
DigitalClock( ClockTimer* t ) : timer_(t)
{ timer_->Attach( bind1st( mem_fun( &DigitalClock::OnUpdateOf ),
this ) ); } --Line2
~DigitalClock()
{ timer_->Detach( bind1st( mem_fun( &DigitalClock::OnUpdateOf ),
this ) ); }
void OnUpdateOf( Subject* timer ) { /* query timer and redraw */ }
private:
ClockTimer* timer_;
};
Here are my questions:
1. mem_fun() generates the unary functor, but bind1st() takes binary
function as its 1st argument, how does Line2 work?
2. Could you please provide stepwise explanation how Line2 being
expanded to fit member function signature of Attach(function<void
(Subject*)> )?
3. is "this" on Line2 a DigitalClock*? DigitalClocl* is not Subject*
for Attach(), isn't?
Thanks.