Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2001-03-18 13:11:24


alexoss_at_[hidden] wrote on 3/18/01 10:46 AM
>7. Are there situations in which use_count() and unique() are
>helpful to have? I've never needed to know this information about my
>old reference-counted pointer class.

I've sometimes used a shared_ptr's unique to help implement a class that
has value semantics, but has refcounted internals for performance reasons:

class ValueSemantics
{
public:
        void modify_me();
        void inspect_me() const;
private:
        shared_ptr<Internals> data_;
};

void
ValueSemantics::modify_me()
{
        if (!data_.unique())
        {
                // get private copy of data
                data_ = shared_ptr<Internals>(new Internals(*data_));
        }
        // Now modify knowing data_ is unique
        // ...
}

void
ValueSemantics::inspect_me() const
{
        // Just use *data_, no need for unique
        // ...
}

-Howard


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