Solved

Proper use of const regarding member functions

Posted on 2003-11-10
1
242 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

9 Experts available now in Live!

Get 1:1 Help Now