473,470 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[C++] Mutable member functions?

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 news:alt.comp.lang.learn.c-c++
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

Jul 22 '05 #1
3 3241

"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:40**************@sbcglobal.net...
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 news:alt.comp.lang.learn.c-c++
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

Jul 22 '05 #2
Thomas Matthews wrote in news:40**************@sbcglobal.net:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
On Sun, 11 Jan 2004 17:54:24 +0000, Thomas Matthews wrote:
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Adam H. Peterson | last post by:
I'm using a private base class as part of the implementation for one of my classes. In my implementation, it holds cached data, and I'd like to be able to call its const member functions from my...
5
by: Adam H. Peterson | last post by:
I'm using a private base class as part of the implementation for one of my classes. In my implementation, it holds cached data, and I'd like to be able to call its const member functions from my...
3
by: Ingo Nolden | last post by:
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...
18
by: Markus.Elfring | last post by:
The C++ language specification provides the key word "mutable" that is not available in the C99 standard. Will it be imported to reduce any incompatibilities?...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.