By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

understanding of const member functions

P: n/a
I have a member function, int GetLogLevel() which I thought I should
change to int GetLogLevel() const - I made the change and it works
fine.

But in the function I am creating buffers and of course the buffers
are filling up with data. So some variable values are changing. So
what is rule for a const member function? Is it that only member
variables cannot change? But local variables inside the function can?
Nov 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 17, 6:12 pm, Angus <anguscom...@gmail.comwrote:
I have a member function, int GetLogLevel() which I thought I should
change to int GetLogLevel() const - I made the change and it works
fine.

But in the function I am creating buffers and of course the buffers
are filling up with data. So some variable values are changing. So
what is rule for a const member function? Is it that only member
variables cannot change? But local variables inside the function can?
At the simplest level, yes - local variables in the function are
allowed to change in a const member function, but the object's member
data cannot be changed.
Basically, the const member function is a promise that the object's
state will not be changed by that function. From my understanding,
this means that if the GetLogLevel() function is called twice, and
there are no calls to a non-const member function (and no data is
otherwise changed), then the second call to GetLogLevel() will have
the same return value as the first.

However it is possible to declare that certain member data can be
changed by const member functions. These are tagged with the
"mutable" keyword. For example, your GetLogLevel() function might be
interested in the number of times it has been called for that object.
It can then keep a mutable member variable in the object and modify
that; however you should keep your promise that two calls to the
GetLevelLog() function will have the same return value.
Nov 17 '07 #2

P: n/a
On Nov 17, 1:12 pm, Angus <anguscom...@gmail.comwrote:
I have a member function, int GetLogLevel() which I thought I should
change to int GetLogLevel() const - I made the change and it works
fine.

But in the function I am creating buffers and of course the buffers
are filling up with data. So some variable values are changing. So
what is rule for a const member function? Is it that only member
variables cannot change? But local variables inside the function can?
Hi Angus

We can discuss about const member functions from two point of view.
First, const member functions are member functions that don't change
the state of objects when they are called. for example:
class C {
int i;
public:
C() : i(0) {}
int Get() const { return i; }
void Update() { i++; }
};

void f()
{
C c;
c.Get();
c.Update(); // state changed
}
Get is const, because it doesn't change the state of object, but
Update is non-const.
such facility, help to compiler to discover bugs/errors early at
compile time like this:
int C::Get() const { return ++i; } // error
Second, using const member functions, we can have const objects and,
just const member functions can be called on const objects:
void g()
{
C o1; // non-const object
o1.Get(); // fine
o1.Update(); // fine
const C o2; // const object
o2.Get(); // fine
o2.Update(); // error
}

In your program, logically the GetLogLevel() should be const, but It
is better to post your code. At last a const member function can
change the value of local variable as far as the data members don't
modified.

Regards,
S. Amrollahi
Nov 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.