Solved

Proper use of const regarding member functions

Posted on 2003-11-10
1
243 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now