[C++] Mutable member functions?

T

Thomas Matthews

Hi,

I understand that a member function can be declared as const
{meaning it doesn't alter the member variables), but can it
be declared as mutable?

I have class that stores its members into a table. The
class has a pointer to the table. The store() method is
constant since the class' members don't change. However,
the table's store method is not constant, which is bringing
up compiler warnings (something like a const method is using
a non-constant function). So I would like to declare the
class's store() method as mutable to solve this situation.

Example:
class Table
{
public:
void store(int value);
void store(string value);
};

class My_Object
{
public:
void store_to_table(void) const;
private:
int quantity;
string name;
Table * p_table;
};

void
My_Object::
store_to_table(void) const
{
// Precondition, p_table is initialized
p_table->store(quantity); // non-const method used here.
p_table->store(name); // non-const method used here.
return;
}

In the above example, the pointer to the table is not
altered, nor are any of the other values. Thus it _can_
be declared const. However, the table's methods aren't.

So is there any way to get around this situation of a
const class method calling an object's non-const member
function and not receive any warnings (let's not modify
the compiler's warning level)?

[Note: crossposted to since it may be of interest to newbies.]
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
N

Nick Hounsome

Thomas Matthews said:
Hi,

I understand that a member function can be declared as const
{meaning it doesn't alter the member variables), but can it
be declared as mutable?
no


I have class that stores its members into a table. The
class has a pointer to the table. The store() method is
constant since the class' members don't change. However,
the table's store method is not constant, which is bringing
up compiler warnings (something like a const method is using
a non-constant function). So I would like to declare the
class's store() method as mutable to solve this situation.

Example:
class Table
{
public:
void store(int value);
void store(string value);
};

class My_Object
{
public:
void store_to_table(void) const;
private:
int quantity;
string name;
Table * p_table;
};

void
My_Object::
store_to_table(void) const
{
// Precondition, p_table is initialized
p_table->store(quantity); // non-const method used here.
p_table->store(name); // non-const method used here.
return;
}

This should compile without warnings - *p_table is not const only p_table
is.
In the above example, the pointer to the table is not
altered, nor are any of the other values. Thus it _can_

What is 'it'? store_to_table or p_table?
be declared const. However, the table's methods aren't.

So is there any way to get around this situation of a
const class method calling an object's non-const member
function and not receive any warnings (let's not modify
the compiler's warning level)?

[Note: crossposted to since it may be of interest to newbies.]
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
R

Rob Williscroft

Thomas Matthews wrote in
Hi,

I understand that a member function can be declared as const
{meaning it doesn't alter the member variables), but can it
be declared as mutable?

No, what ever your problem is it isn't this.
I have class that stores its members into a table. The
class has a pointer to the table. The store() method is
constant since the class' members don't change. However,
the table's store method is not constant, which is bringing
up compiler warnings (something like a const method is using
a non-constant function).

Which compiler ?
So I would like to declare the
class's store() method as mutable to solve this situation.

Example:
class Table
{
public:
void store(int value);
void store(string value);
};

class My_Object
{
public:
void store_to_table(void) const;
private:
int quantity;
string name;
Table * p_table;
};

void
My_Object::
store_to_table(void) const
{
// Precondition, p_table is initialized

The effective type of p_table is Table * const, *NOT* Table const *.
i.e this should (and does) compile.
p_table->store(quantity); // non-const method used here.
p_table->store(name); // non-const method used here.
return;
}

In the above example, the pointer to the table is not
altered, nor are any of the other values. Thus it _can_
be declared const. However, the table's methods aren't.

It isn't declared const.
So is there any way to get around this situation of a
const class method calling an object's non-const member
function and not receive any warnings (let's not modify
the compiler's warning level)?

You, should cut down the real code, just enough to give the
warning/error you are having a problem with and post that.


Rob.
 
J

Josh Sebastian

I understand that a member function can be declared as const
{meaning it doesn't alter the member variables), but can it
be declared as mutable?

No, there wouldn't be any need for that. The only sensible meaning would
be that a mutable member function would be a function that could modify
the members of a const object. But that makes no sense when you think
about why an object might be const: it could be in read-only memory, which
would prevent your "mutable function" from doing anything useful.
I have class that stores its members into a table. The
class has a pointer to the table. The store() method is
constant since the class' members don't change. However,
the table's store method is not constant, which is bringing
up compiler warnings (something like a const method is using
a non-constant function).

I find it strange that you say, "the class' members don't
change". I mean, the table is part of the logical state of the object,
right? And you're *store*ing something in the table, which changes its
state. So the logical state of your object /does/ in fact change. So the
function shouldn't be const. I'm assuming, of course, that the table is
part of the logical state of the object. If not, you might need to declare
the table as mutable.

Josh
 

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,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top