On Nov 17, 1:12 pm, Angus <anguscom...@gm ail.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