Boost logo

Boost Users :

From: John Harris (john.harris_at_[hidden])
Date: 2003-04-30 11:33:11


--- In Boost-Users_at_[hidden], Hossein Haeri <powerprogman_at_y...>
wrote:
> Dear all,
>
> I'm a newbie in boost!
>
> I want to know what the following means:
>
> int a = 5;
> const boost::shared_ptr<int> sp(&a);
>
> Does it mean that the following line is an error?
>
> *sp = 7;
>
> If so, what are the guarantees? Is there any
> documented
> guarantee? (I couldn't find anything in the
> documentation. Excuseme if I'm careless!)

I'd say you could call any of the 'const' member functions (e.g.
operator*(), operator->(), get(),...,etc), but the returned type is a
non-const pointer.

Since operator*() is a const member function, it is legal on a const
shared_ptr<T>. The '=', then, applies to the non-const shared ptr
returned, not to the shared_ptr<T> object, itself.

Note also, that you're supposed to initialized shared_ptr<T> with a
*dynamically* allocated T (use new), because it will call 'delete' on
the ptr when the last reference disappears. So you are wrong to
initialize it with the address of a stack variable.

jh


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