Solved

Proper use of const regarding member functions

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

623 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