473,549 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1919
On Nov 17, 6: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?
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...@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
Nov 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3474
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 non-zero index is returned. Below is my attempt, which generates compiler errors: namespace Reference {
20
2471
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 keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
2
3623
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
15
2104
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 question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other...
5
1626
by: noobzillaking | last post by:
If I have the following code: struct Pencil { int Sharpness; }; class random { public: Pencil& GetPencil() const {return p;}
2
7614
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 mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing...
23
2299
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
3740
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 there a provision to indicate that a static member function does not modify the static data members but only uses their values ? Kindly explain....
8
4411
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 looks like this header file
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7971
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3509
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.