473,747 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ 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.l ang.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.l earn.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 3257

"Thomas Matthews" <Th************ *************@s bcglobal.net> wrote in
message news:40******** ******@sbcgloba l.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.l ang.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.l earn.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******** ******@sbcgloba l.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
3059
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 nonconst members. Is there a way to get something like "mutable inheritance" to accomplish this?
5
1316
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 nonconst members. Is there a way to get something like "mutable inheritance" to accomplish this?
3
2360
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 iterators are around, any deleted item remains as NULL entry in the inner std::list This way I had to remove const from most of my member functions. Is is
18
2596
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? http://david.tribble.com/text/cdiffs.htm#C99-cpp-keyword http://www.inf.uni-konstanz.de/~kuehl/c++-faq/const-correctness.html#faq-18.13 Regards, Markus
0
8979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.