POV-Ray : Newsgroups : povray.off-topic : Job Interview : Re: Job Interview Server Time
29 Jul 2024 12:15:06 EDT (-0400)
  Re: Job Interview  
From: Warp
Date: 5 Mar 2012 13:02:42
Message: <4f54ffc1@news.povray.org>
Darren New <dne### [at] sanrrcom> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.