const member functions

D

Denver Dash

Hi. I heard once that there is a way to allow a const member function to
alter the value of a member if the member is declared in some way. Can
someone tell me how to do that, or is this impossible?

For example:

class foo
{
specialdeclaration int index;
void bar() const
{
index = 0; \\ ok because index has been declared in a special way.
}
};

thanks,
denver.
 
T

Thomas Wintschel

Denver Dash said:
Hi. I heard once that there is a way to allow a const member function to
alter the value of a member if the member is declared in some way. Can
someone tell me how to do that, or is this impossible?

For example:

class foo
{
specialdeclaration int index;
void bar() const
{
index = 0; \\ ok because index has been declared in a special way.
}
};

thanks,
denver.

mutable
 
R

Ron Natalie

Thomas Wintschel said:
Yes, but you must be very careful NEVER to actually make an const foo. This is
a problem with trying to use mutable.

const foo x;

x.bar(); // well formed, but undefined behavior.
 
C

Christoph Rabel

Ron said:
Yes, but you must be very careful NEVER to actually make an const foo. This is
a problem with trying to use mutable.

const foo x;

x.bar(); // well formed, but undefined behavior.

No.

See 7.1.5.1.
Its perfectly save to modify a mutable member of a const object.

mfg

Christoph
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top