Topic: Why do we have 'mutable'?


Author: "Solosnake" <solosnake@solosnake.without_this.freeserve.co.uk>
Date: Sat, 23 Mar 2002 22:10:17 GMT
Raw View
Hello

Could anyone explain to me the rationale behind including 'mutable' in the
language standards? 'A storage class specifier which allows const member
functions to modify another member.' But why? If the function tampers with
other member functions/data, then it is violating its agreement to be const.
I thought this was the type of specialised keywords and functionality that
the C++ standards have tried, with good success, to avoid. Seeing as this
aberration has been admitted I assume there is a very good reason, which I
am curious to hear.

Thanks,

D. Stockdale



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Pete Becker <petebecker@acm.org>
Date: Sat, 23 Mar 2002 23:47:21 GMT
Raw View
Solosnake wrote:
>
> Hello
>
> Could anyone explain to me the rationale behind including 'mutable' in the
> language standards? 'A storage class specifier which allows const member
> functions to modify another member.' But why? If the function tampers with
> other member functions/data, then it is violating its agreement to be const.
> I thought this was the type of specialised keywords and functionality that
> the C++ standards have tried, with good success, to avoid. Seeing as this
> aberration has been admitted I assume there is a very good reason, which I
> am curious to hear.
>

You use 'mutable' for things like internal data caches, which don't
affect what a class's member functions do, but can affect how
efficiently they do them.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sun, 24 Mar 2002 02:26:34 GMT
Raw View
In article <a7iidf$4gc$1@news7.svr.pol.co.uk>, Solosnake
<solosnake@solosnake.without_this.freeserve.co.uk> writes
>Could anyone explain to me the rationale behind including 'mutable' in the
>language standards? 'A storage class specifier which allows const member
>functions to modify another member.' But why? If the function tampers with
>other member functions/data, then it is violating its agreement to be const.
>I thought this was the type of specialised keywords and functionality that
>the C++ standards have tried, with good success, to avoid. Seeing as this
>aberration has been admitted I assume there is a very good reason, which I
>am curious to hear.

Because it is entirely possible for a class to have data that does NOT
contribute to the state of an object. The classic example is where the
class designer supplies a mechanism for caching a result the first time
it is evaluated.

For example when designing a Rational number class the state of an
instance is fully defined by the numerator and denominator however it
seems desirable to allow an instance to cache the value as a double the
first time it is evaluated. Note that division is always an expensive
operation and numerical specialists would not want to take the hit for
any unnecessary evaluation (most particularly if BigInts were being used
for the numerator & denominator.)

How does the existence of mutable violate the concept of a const state?


--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Roger Orr" <rogero@howzatt.demon.co.uk>
Date: Sun, 24 Mar 2002 22:10:41 GMT
Raw View
Solosnake wrote in message ...
>Hello
>
>Could anyone explain to me the rationale behind including 'mutable' in the
>language standards? 'A storage class specifier which allows const member
>functions to modify another member.' But why? If the function tampers with
>other member functions/data, then it is violating its agreement to be
const.


Sometimes this is expressed as the difference between 'logical' const and
'physical' const.

That is, the const method does not change the object's external state
(logical const) but does change its bit pattern in memory (physical const)
Common examples of its use are internal caching and statistics counters.

--
Roger Orr
MVP in C++ at www.brainbench.com


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]