473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

advice on using mutable

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
Jul 23 '05 #1
3 2339
"Ingo Nolden" <in**********@SPAMrecurdyn.org> wrote in message
news:d0**********@svr7.m-online.net...
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).

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Jul 23 '05 #2
"Ingo Nolden" <in**********@SPAMrecurdyn.org> wrote in message
news:d0**********@svr7.m-online.net...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #3
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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
12
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
12
by: Water Cooler v2 | last post by:
Are JavaScript strings mutable? How're they implemented - 1. char arrays 2. linked lists of char arrays 3. another data structure I see that the + operator is overloaded for the string class...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
1
by: Let_Me_Be | last post by:
Hi, I'm kinda worndering how the mutable keyword works. Because of speed, I added value caching into my base class, and the only posible way to implement this seemed to be adding the mutable...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.