|
Boost : |
Subject: Re: [boost] shared_ptr<>: why no operator[] ? [SEC=UNCLASSIFIED]
From: Levy, Gabriel (Contractor) (Gabriel.Levy_at_[hidden])
Date: 2011-07-27 05:35:04
UNCLASSIFIED
Note!!!
The following statement
boost::shared_ptr<int> p(new int(42));
is NOT allocating an array of int, it allocates only 1 int and
initialises it to the value of 42.
To instantiate an array the statement would have to use .... (new
int[42])
So the doco you refered to IS correct.
Hope this clarifies it...
Gabe Levy
Senior Software Engineer, Contractor, Estrings Pty Ltd
-----Original Message-----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of dgwsoft
Sent: Wednesday, 27 July 2011 6:37 PM
To: boost_at_[hidden]
Subject: Re: [boost] shared_ptr<>: why no operator[] ?
Neil Groves-3 wrote:
>
> On Tue, Jul 26, 2011 at 11:39 PM, dgwsoft <gareth_at_[hidden]>
> wrote:
>
>> boost::shared_ptr<int> p(new int(42));
>>
>> To access elements of the array I can do:
>>
>> p.get()[10] = 7;
>>
>> But wouldn't it be nicer to do:
>>
>> p[10] = 7; // ?
>>
>> So: why is no T& operator[](int) defined for shared_ptr ? I'm just
>> interested in the design reasons for this.
>>
>>
> This isn't guaranteed to properly destroy the array. It will call
> delete, rather than delete[].
> This is why there is also boost::shared_array which is what you should
> be using and has an operator[].
>
Thanks, Neil, that makes perfect sense. One should never do:
boost::shared_ptr<int> p(new int(42));
In which case, someone should look at this page:
http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm#examp
le
(under Thread Safety) which is where I copied that code from.
cheers
Gareth
-- View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-why-no-operator-tp3697208p 3697938.html Sent from the Boost - Dev mailing list archive at Nabble.com. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost IMPORTANT: This email remains the property of the Department of Defence and is subject to the jurisdiction of section 70 of the Crimes Act 1914. If you have received this email in error, you are requested to contact the sender and delete the email.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk