Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-11-09 05:58:16


Martin Wille wrote:
> Aleksey Gurtovoy wrote:
> ...
> > widget w2;
> > w.enable(boost::bind(&widget::is_enabled, &w2)); // here!
> > w2.enable(false);
> > assert(!w.is_enabled()) // !
> > }
> >
> > Pure & beautiful, IMO.
>
> Consider a widget class that performs some action on
> enabling/disabling (like dimming a button). In that case the
> action would be performed for w2 but not for w.

Oh, sorry for a confusion, 'enable' was a bad name for the method because I
didn't mean that semantics (and the whole example just wouldn't make much
sense).

It should have been 'is_enabled(bool_arg_t)' instead, because I was thinking
in terms of the model where the widget implementation itself _queries_ the
'widget' object's properties to perform the appropriate system actions, like
dimming a button.

So, the corrected example would be:

    class widget
    {
    public:
        // ...
        typedef boost::function<bool ()> bool_arg_t;
        
        self_t& is_enabled(bool_arg_t a_enabled); // note the signature!
        bool is_enabled() const;
    };

    int main()
    {
        widget w;
        w.is_enabled(true); // ordinary syntax/semantics
        assert(w.is_enabled())
        
        widget w2;
        w.is_enabled(boost::bind(&widget::is_enabled, &w2)); // here!
        w2.is_enabled(false);
        assert(!w.is_enabled()) // !
    }

I hope the name change makes the semantics of the above clearer.

Aleksey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk