Hi All,

Mutable keyword can be used only with class and structure data members, that
are 'non-static' and 'non-const'.

Can someone tell me what is the reason behind this constraint?

Why 'static' and 'const' data members cannot be made mutable?

Thanks in advance

Re: C++: mutable keyword by Doug

Doug
Wed Dec 05 19:58:57 PST 2007

On Wed, 5 Dec 2007 19:36:11 -0800, Ken <Ken@discussions.microsoft.com>
wrote:

>Hi All,
>
>Mutable keyword can be used only with class and structure data members, that
>are 'non-static' and 'non-const'.
>
>Can someone tell me what is the reason behind this constraint?
>
>Why 'static' and 'const' data members cannot be made mutable?

The purpose of declaring a variable mutable is to prevent it from becoming
implicitly const inside a const member function. It would be bizarre to
explicitly declare a member variable const and then want its constness to
be undone inside const member functions; this use of mutable would not be
undoing the effect of the const member function, it would be undoing your
original declaration. IOW, if you don't want the member variable to be
const, you shouldn't declare it const in the first place. As for static
member data, it is not affected inside const member functions, so it
doesn't need to be mutable.

--
Doug Harrison
Visual C++ MVP

Re: mutable keyword by Igor

Igor
Wed Dec 05 20:00:25 PST 2007

"Ken" <Ken@discussions.microsoft.com> wrote in message
news:B326F569-E24F-4D74-BE9A-328AF917E00F@microsoft.com
> Mutable keyword can be used only with class and structure data
> members, that are 'non-static' and 'non-const'.
>
> Can someone tell me what is the reason behind this constraint?
>
> Why 'static' and 'const' data members cannot be made mutable?

The point of 'mutable' is that even if the object itself is declared
const, the mutable fields still can be modified:

class C {
int x;
mutable int y;
public:
void f() const {
x = 1; // compiler error - can't modify const variable
y = 2; // OK
}
};

const C c;
c.f();

On the other hand, if you declare a field const, you cannot modify it
even if the object itself is non-const. So 'const' and 'mutable' are
opposites, it makes no sense to put both on the same field.

As to static: again, the reason to declare a field 'mutable' is to be
able to modify it even in an object declared const. But a static field
is not tied to any particular object, so it doesn't magically become
const if you declare the containing object const (there's no containing
object). So there's no reason to declare it mutable: just don't make it
const, and you can always modify it.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: mutable keyword by David

David
Thu Dec 06 02:25:58 PST 2007


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:u0g%23Iy7NIHA.3516@TK2MSFTNGP02.phx.gbl...

> As to static: again, the reason to declare a field 'mutable' is to be able
> to modify it even in an object declared const. But a static field is not
> tied to any particular object, so it doesn't magically become const if you
> declare the containing object const (there's no containing object). So
> there's no reason to declare it mutable: just don't make it const, and you
> can always modify it.

Are you saying that a const member function of a class *can* change static
data members of the class?

[I ask out of interest: it isn't something I've ever thought to try.]

[I do use mutable data members, though I know that some think of them as a
betrayal of object orientation. I think of them as sort of 2nd class
citizens: things which it is convenient to have there, but whose value does
not really change the nature of the object. For example some of my objects
have CDC * members which are mutable, so that it can acquire a DC and draw
itself and then lose the DC - it saves passing them as arguments through
multiple tiers of drawing code.]

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm


Re: C++: mutable keyword by Tom

Tom
Thu Dec 06 04:05:57 PST 2007

Ken wrote:
> Hi All,
>
> Mutable keyword can be used only with class and structure data members, that
> are 'non-static' and 'non-const'.
>
> Can someone tell me what is the reason behind this constraint?
>
> Why 'static' and 'const' data members cannot be made mutable?

The mutable keyword allows a member variable to be modified via a const
object, reference or pointer (note that in a const member function, the
implicit 'this' pointer is const). Static members aren't accessed via an
object, and const members can't be modified in any case regardless of
whether the enclosing object is const, so mutable is not applicable in
either of those cases.

Tom

Re: mutable keyword by Tom

Tom
Thu Dec 06 04:09:35 PST 2007

David Webber wrote:
>
> "Igor Tandetnik" <itandetnik@mvps.org> wrote in message
> news:u0g%23Iy7NIHA.3516@TK2MSFTNGP02.phx.gbl...
>
>> As to static: again, the reason to declare a field 'mutable' is to be
>> able to modify it even in an object declared const. But a static field
>> is not tied to any particular object, so it doesn't magically become
>> const if you declare the containing object const (there's no
>> containing object). So there's no reason to declare it mutable: just
>> don't make it const, and you can always modify it.
>
> Are you saying that a const member function of a class *can* change
> static data members of the class?

Of course, just as a const member function can change anything that any
function can change, providing it isn't accessing it via the implicit
this pointer. The only special thing about implementing a const member
function is that the implicit this pointer is const.

Tom

Re: mutable keyword by David

David
Thu Dec 06 06:26:50 PST 2007


"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> wrote in message
news:uMFPfDAOIHA.820@TK2MSFTNGP06.phx.gbl...

>> Are you saying that a const member function of a class *can* change
>> static data members of the class?
>
>.... The only special thing about implementing a const member function is
>that the implicit this pointer is const.

Ah thanks. Put like that, it is obvious. :-) I had just never
thought about it before.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm