Boost logo

Boost :

From: Jens Maurer (Jens.Maurer_at_[hidden])
Date: 2001-07-16 15:23:01


John Max Skaller wrote:
> I seek your opinion on get/set vs. get/ref.

> When the data type is mutable, there are two choices.
> The simplest choice is to provide set methods.

> For Algol like languages, we could also provide references:

> Clearly, ref methods are more powerful and more efficient,
> but on the other hand they expose the underlying implementation
> and prevent hooking changes to the mutable state.
>
> What's the best technique?

Unless there is a very good, specific reason to use a "ref"
member returning a reference, I'd always prefer a "set"
interface for writing data into an object.

C++ iterators have gone the "reference" way (i.e. operator*
usually returns a reference), and it causes grief in various
aspects: std::vector<bool> is not a container any more,
because operator* tried to emulate a reference by a proxy
and failed to meet the container requirements on the way.
Differentiating between reading from and writing to the result
of operator* on an iterator requires delicate proxy objects,
isn't fully transparent, and possibly also violates the
container requirements.

Differentiating between reading and writing is often useful,
e.g. when talking to databases, checking constraints,
choosing locking strategies.

(There was at least one good reason for iterators going the
"reference" way: They imitate raw pointers and therefore
feel more comfortable and natural to some programmers.)

Jens Maurer


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk