473,569 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2351
"Ingo Nolden" <in**********@S PAMrecurdyn.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**********@S PAMrecurdyn.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
6311
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the...
12
6355
by: Kjetil Kristoffer Solberg | last post by:
What is a mutable struct? regards Kjetil Kristoffer Solberg
12
3730
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 and hence it is possible to do: txt += "foo bar";
12
3133
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
2270
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 keyword to cache representing members. The thing is, how will the program behave when the mutable members are modified in a const class variable....
10
2557
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 is that it's not destroyed, but I'm not sure. I have the following program: #include<iostream>
2
4255
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 whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for...
11
2273
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
24
2489
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 finally, and painfully, fade away than another one starts up. I suggest that Python should raise warnings.RuntimeWarning (or similar?) when a function...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.