Boost logo

Boost Users :

From: Darin Adler (darin_at_[hidden])
Date: 2003-04-30 11:29:01


On Wednesday, April 30, 2003, at 08:28 AM, Hossein Haeri wrote:

> 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;

No, if you want that then you need

     boost::shared_ptr<int const> sp2(&a);

The way const works with shared_ptr is similar to the way it works with
plain pointers. You can have a constant pointer, which means the
pointer can't be modified, or a pointer to constant, which means the
object that's pointed to can't be modified.

> If so, what are the guarantees? Is there any documented guarantee?

The fact that sp is constant means you can't do this:

     int b = 6;
     sp = &b;

The fact that sp2 is constant means you can't do this:

     *sp2 = 7;

Hope that helps.

     -- Darin


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