Solved

Proper use of const regarding member functions

Posted on 2003-11-10
1
246 Views
Last Modified: 2010-04-01
When defining a member function one can use the const keyword in a nubmer of places. I would like to check on the subtleties of location.

If my method merely returns an integer (say a counter) then I could define it thus:

int GetCounter() {return fCounter;}

If I want to ensure that the function does no change the member variables in any way I can add const

int GetCounter() const {return fCounter;}

What I would like to find out about is what it means exactly to use the const keyword in the following three cases:

int GetCounter() const;
const int GetCounter() {return fCounter;}
const int GetCounter() const {return fCounter;}

Thanks
0
Comment
Question by:marknewlyn
1 Comment
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 125 total points
ID: 9712984
int GetCounter() const;

Ths member function cannot modify any of the data members. It also cannot give a call to any of non-const member functions [compile time error results]
A const member function is safe to call with both const and non-const objects. Thus, you could think of it as the most
general form of a member function (and because of this, it is unfortunate that member functions do not automatically default to const). Any function that doesn’t modify member data should be declared as const, so it can be used with const objects.



const int GetCounter() {return fCounter;}

It returns a const integer.
For built-in types, it doesn’t matter whether you return by value as a const, so you should avoid confusing the client programmer and leave off the const when returning a built-in type by value. Returning by value as a const becomes important when you’re dealing with user-defined types. If a function returns a class object by value as a const, the return value of that function cannot be an lvalue (that is, it cannot be assigned to or otherwise modified).

const int GetCounter() const {return fCounter;}

Combine the above two explanations








0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question