Solved

Proper use of const regarding member functions

Posted on 2003-11-10
1
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

739 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