advice on using mutable

I

Ingo Nolden

Hi,


I try to use const where ever appropriate.
In a collection class I am counting the iterators that are out. The
counter decrements when an iterator leaves scope or is 'Dispose( )'d
While iterators are around, any deleted item remains as NULL entry in
the inner std::list

This way I had to remove const from most of my member functions. Is is
good style to make the counter mutable to avoid this, since the counter
is not part of the main purpose of the class and its actual 'value'?

The same applies to reference counting on ordinary objects. I use
intrusive smart pointers, that increment and decrement a counter which
is in the object itself. I couldn't change the counters value if the
object is a const. Should the counter be mutable here?

thanks in advance
 
I

Ivan Vecerina

Ingo Nolden said:
I try to use const where ever appropriate.
In a collection class I am counting the iterators that are out. The
counter decrements when an iterator leaves scope or is 'Dispose( )'d
While iterators are around, any deleted item remains as NULL entry in the
inner std::list

This way I had to remove const from most of my member functions. Is is
good style to make the counter mutable to avoid this, since the counter is
not part of the main purpose of the class and its actual 'value'?

The same applies to reference counting on ordinary objects. I use
intrusive smart pointers, that increment and decrement a counter which is
in the object itself. I couldn't change the counters value if the object
is a const. Should the counter be mutable here?

Yes. These would both be legitimate uses for 'mutable'.

Another common reason to use mutable is for cached data (e.g. a 'getter'
may store info about recent queries to re-execute them faster).
 
C

Cy Edmunds

Ingo Nolden said:
Hi,


I try to use const where ever appropriate.
In a collection class I am counting the iterators that are out. The
counter decrements when an iterator leaves scope or is 'Dispose( )'d
While iterators are around, any deleted item remains as NULL entry in the
inner std::list

This way I had to remove const from most of my member functions. Is is
good style to make the counter mutable to avoid this, since the counter is
not part of the main purpose of the class and its actual 'value'?

The same applies to reference counting on ordinary objects. I use
intrusive smart pointers, that increment and decrement a counter which is
in the object itself. I couldn't change the counters value if the object
is a const. Should the counter be mutable here?

thanks in advance

A const method doesn't change the state of the object. However, the state of
the object is an abstract concept, not at all the same as the private data
within the class. What constitutes the state of the object is up to the
object's designer. The applications of mutable you describe certainly sound
OK to me.
 
S

shabbir

The concept of mutable is very important. I give you an example of the
scenario

Lets assume that you are having a getty function and that function is
assumed to be constant and so user expects that nothing is changing in
the function. But for some reason there is a need of some elementary
kind of Audit trail as who accessed the data and what time so we need
to set the Date Accessed / Accesses By data of the const function and
so we need variables that does not have influence of the User interface
as Mutable and can be changed in the getty functions also.

So I think you are right saying that
"What constitutes the state of the object is up to the object's
designer."

but actually if we reframe it its how well the object has been designed
to give the user a better interface like he is sure its constant
functions is more important and some values that are for reference they
can b changed.

Thanks
Shabbir Bhimani
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top