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 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
"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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
by: Kjetil Kristoffer Solberg |
last post by:
What is a mutable struct?
regards
Kjetil Kristoffer Solberg
|
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";
|
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.
|
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....
| |
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>
|
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...
|
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;
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |