Boost logo

Boost Users :

From: Niklas Wiberg (nwlists-boostusers_at_[hidden])
Date: 2004-12-09 10:04:43


Hi,

I have a question that I believe is more a matter of taste than one of
functional correctness...

Say that I have a model (see Basket below) that formally owns the
heap-objects (see Apple below).
Basket has a function that is merely a query (hasApple) and involves no
ownership-changes.
Also assume that there is good reason that Apple:s are allocated on the
heap...

On one hand, I can see how using a weak_ptr as the parameter type would
signal to users that hasApple has no intention of modifying ownership of
the pointed-to object.
On the other hand, in order to actually use the pointee (search for it
in this case), hasApple must convert the weak_ptr to shared_ptr (lock()
or shared_ptr constructor), so it would seem more pragmatic to just use
shared_ptr as the parameter type.

class Apple;

class Basket {
     std::vector< boost::shared_ptr<Apple> > m_contents;
     bool hasApple( shared OR weak_ptr<Apple> a); // no transfer

     ...more functions
};

Is there a "good style" concensus on how to choose smart pointer type
for function arguments among boost smart pointer users?

Best regards,
Niklas Wiberg


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net