473,378 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

understanding of const member functions

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
2 1902
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
5
by: noobzillaking | last post by:
If I have the following code: struct Pencil { int Sharpness; }; class random { public: Pencil& GetPencil() const {return p;}
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
6
by: subramanian100in | last post by:
why can't a static member function be declared as const ? We can declare a non-static member function as const, to indicate that it does not modify the non-mutable data members. In the same way, is...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.