|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> On 3/4/2012 14:33, Warp wrote:
> > return mData[index];
> Given the previous discussion, I'm betting this is the error Warp was
> talking about - returning the address of a variable that gets disposed when
> this object gets disposed.
No. It's not returning a reference to a local variable. Returning a
reference to a member variable is standard practice (for example all
standard data containers do that.) You wouldn't be able to modify the
members of the array using operator [] from the outside otherwise.
(In the const version of the operator it would be possible to return
the element by value. However, this is not usually done because the
members might be heavy to copy.)
(Yes, it would be possible for the calling code to define a reference
or pointer to the array element and keep it for longer than the lifetime
of the array. However, that kind of error is no different from doing it
with raw arrays or any other type of data container. A class acting as
a wrapper for a data container cannot guard the calling code from this.)
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |