mutable v's non const

P

Paul

Hi groupies.

What is the meaning of the "mutable" word in C++ circles?
Mutable apparently means "it can change". So why are the terms "mutable" and
"non-mutable" used instead of const and non-const and in what way are they
different?
 
R

red floyd

Hi groupies.

What is the meaning of the "mutable" word in C++ circles?
Mutable apparently means "it can change". So why are the terms "mutable"
and "non-mutable" used instead of const and non-const and in what way
are they different?

A mutable class member may be modified even if the object is const.

e.g.:

class Example {
public:
void good() const {
mutable_int = 7;
}
void bad() const {
non_mutable_int = 7; // ERROR
}
Example() :
mutable_int(0), non_mutable_int(0)
{
}
private:
mutable int mutable_int;
int non_mutable_int;
};

Example::good() will compile, and is fine.
Example::bad() will not compile because it's modifying a
non-mutable member of a const object.
 
P

Paul

red floyd said:
A mutable class member may be modified even if the object is const.

e.g.:

class Example {
public:
void good() const {
mutable_int = 7;
}
void bad() const {
non_mutable_int = 7; // ERROR
}
Example() :
mutable_int(0), non_mutable_int(0)
{
}
private:
mutable int mutable_int;
int non_mutable_int;
};
Thanks for the example.

Why would you make such a function const in the first place?
If its not const why mark it as const?
 
I

Ian Collins

Thanks for the example.

Why would you make such a function const in the first place?
If its not const why mark it as const?

So it can be called on a const instance of an Example.
 
D

Daniel Weber

Am 10.07.2011 10:58, schrieb Paul:
Why would you make such a function const in the first place?
If its not const why mark it as const?

Consider the difference between technically const and semantically const.

You might e.g. have a class (the program's configuration settings) which
needs to synchronize parallel calls to some/all methods and therefore
has a helper attribute which is used for the synchronization. When one
of the synchronized method is semantically const - it doesn't change any
of the configuration settings - but it has to change the helper attribute...

Bye,
Daniel
 
P

Paul

Daniel Weber said:
Am 10.07.2011 10:58, schrieb Paul:

Consider the difference between technically const and semantically const.

You might e.g. have a class (the program's configuration settings) which
needs to synchronize parallel calls to some/all methods and therefore has
a helper attribute which is used for the synchronization. When one of the
synchronized method is semantically const - it doesn't change any of the
configuration settings - but it has to change the helper attribute...
It seems to be a bit of a cheat to allow a modifiable data member of a const
object.
I did a little research , it seems to be a case that this only applies to
non static members. Handy for debugging according to the first couple of
pages I read about it.

After further thinking about your explanation about a helper attribute it
seems pretty much spot on.
 
P

Paul

Leigh Johnston said:
I think the terms "logically const" and "physically const" are more
cromulent.
Daniels terms seem more fitting to me. I did see the term "logically
constant" in one of the articles I read to be fair to you.
 
P

Paul

Paul said:
It seems to be a bit of a cheat to allow a modifiable data member of a
const object.
I did a little research , it seems to be a case that this only applies to
non static members.

Maybe I am incorrect about this only being applicable to nonstaic data
members. Can anyone clarify if the mutable qualifier can be used on objects
other than nonstatic class members?
 
P

Paul

Werner Erasmus said:
What would be the point of making them applicable to static
class members? The keyword mutable only makes sense during
the execution of const (member) functions. Par 7.1.1:10 (n3242)
makes it clear that mutable only applies to names of class data
members, and not to members declared const or static. 7.1.1:11
elaborates on its precise purpose.

I guess I was just wondering if this sort of thing was possible:

struct X{ const int i;}
mutable X anX;
anX.i =5;
 
P

Paul

Leigh Johnston said:
FFS: TIAS; or to put it another way stop wasting bandwidth (and people's
time) on Usenet.
If you think it's a waste of bandwidth then why add to the bandwidth wastage
with your plop?
 
P

Paul

Paul said:
I guess I was just wondering if this sort of thing was possible:

struct X{ const int i;}
mutable X anX;
anX.i =5;
.....or if there were any situations where mutable could be applied other
than a nonstatic data member.

Please ignore Leighs rude comments as he has a tendancy to be nasty
especially when he bears a grudge or three ;-)

Anyway thanks for posting of the quote from the standard as that seems to
comfirm my original assumption.
 
P

Paul

Leigh Johnston said:
On 11/07/2011 17:43, Paul wrote:
[snip]
Please ignore Leighs rude comments as he has a tendancy to be nasty
especially when he bears a grudge or three ;-)

No grudges; you're just an arsehole.
And what have I done to you to deserve that?
All I have done is prove you wrong about things on a few occasions for which
you seem to bear a grudge. <shrug>
 
P

Paul

Leigh Johnston said:
Leigh Johnston said:
On 11/07/2011 17:43, Paul wrote:
[snip]


Please ignore Leighs rude comments as he has a tendancy to be nasty
especially when he bears a grudge or three ;-)


No grudges; you're just an arsehole.
And what have I done to you to deserve that?
All I have done is prove you wrong about things on a few occasions for
which you seem to bear a grudge. <shrug>

You are an arsehole because, for example, you constantly claim to have
proven others wrong when you haven't (just like you did just now).
I have proved you wrong on many occassions , you just do not accept it.
You are also actively homophobic which also counts towards you being an
arsehole.
:-S
WTF does homophobia have to do with anything and so what if I was
homophobic. I'd rather be homophobic than homosexual.


You are also an arsehole because you are unable to take criticism, admit
to gaps in your knowledge and apologize for your mistakes.

Not at all. That's more like a description of yourself.
 
J

Juha Nieminen

Paul said:
Why would you make such a function const in the first place?
If its not const why mark it as const?

A practical example where you need to modify the mutable member variable
of a const object is a reference-counting smart pointer which uses double
linking for the reference counting.

Such a smart pointer, instead of actually using an actual integral
reference count, has a 'prev' and 'next' pointer as members, like a
doubly-linked list. Whenever two smart pointers point to the same object
(in which case the "reference count" would be larger than 1), they are
linked together with those pointers (possibly via a larger chain of
smart pointer objects, if there are more than two pointing to the same
object). The advantage of this is that you don't need to allocate a
separate reference count (which is more efficient), nor require one to
be in the managed object (which means the smart pointer is not intrusive
and thus works with any type). The disadvantage is that the size of the
smart pointer is that of three pointers.

The 'prev' and 'next' pointers have to be mutable. There's no way around
it. That's because the copy constructor and assignment operators must take
the parameter as const:

TehSmartPointer::TehSmartPointer(const TehSmartPointer& rhs):
object(rhs.object)
{
// Insert 'this' into the linked list where 'rhs' is. It has to modify
// 'rhs.prev' and 'rhs.prev->next', which have to be mutable.
}
 
O

Oliver Jackson

On 11/07/2011 18:04, Paul wrote:
On 11/07/2011 17:43, Paul wrote:
[snip]
Please ignore Leighs rude comments as he has a tendancy to be nasty
especially when he bears a grudge or three ;-)
No grudges; you're just an arsehole.
And what have I done to you to deserve that?
All I have done is prove you wrong about things on a few occasions for
which you seem to bear a grudge. <shrug>
You are an arsehole because, for example, you constantly claim to have
proven others wrong when you haven't (just like you did just now).

I have proved you wrong on many occassions , you just do not accept it.
 You are also actively homophobic which also counts towards you beingan
arsehole.

:-S
WTF does homophobia have to do with anything and so what if I was
homophobic. I'd rather be homophobic than homosexual.

Paul, please post only when you have something on-topic to contribute.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top