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
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