understanding of const member functions

A

Angus

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

alan

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

Saeed Amrollahi

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
 

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
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top