const Member Functions in C++

By: Emiley J Emailed: 1757 times Printed: 2445 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

If you declare a class method const, you are promising that the method won't change the value of any of the members of the class. To declare a class method constant, put the keyword const after the parentheses but before the semicolon. The declaration of the constant member function SomeFunction() takes no arguments and returns void. It looks like this:

void SomeFunction() const;

Accessor functions are often declared as constant functions by using the const modifier. The Cat class has two accessor functions:

void SetAge(int anAge);
int GetAge();

SetAge() cannot be const because it changes the member variable itsAge. GetAge(), on the other hand, can and should be const because it doesn't change the class at all. GetAge() simply returns the current value of the member variable itsAge. Therefore, the declaration of these functions should be written like this:

void SetAge(int anAge);
int GetAge() const;

If you declare a function to be const, and the implementation of that function changes the object by changing the value of any of its members, the compiler flags it as an error. For example, if you wrote GetAge() in such a way that it kept count of the number of times that the Cat was asked its age, it would generate a compiler error. This is because you would be changing the Cat object by calling this method.

 


NOTE: Use const whenever possible. Declare member functions to be const whenever they should not change the object. This lets the compiler help you find errors; it's faster and less expensive than doing it yourself.

It is good programming practice to declare as many methods to be const as possible. Each time you do, you enable the compiler to catch your errors, instead of letting your errors become bugs that will show up when your program is running.


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

u said that const function cant change object.can they change a static member of object?
itry that.and it can change it!why?

class c{
static int d;
void f() const {d=0;} //!
}
int c::d;
...
it has no error


View Tutorial          By: z at 2010-05-09 12:02:17
2. View Comment

The static variables are not in object scope instead it is of class scope.i.e here only one 'd' will be there irrespective of the number of object of this class.

View Tutorial          By: Karthick at 2011-06-29 06:55:17

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Function overloading in C++
The Birth, history and need for C++
Dot (.) vs Arrow (->) to access data members in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Using cout.width() in C++
Using cout in C++
Calculating total based on the given quantity and price in C++
strcat() and strncat() sample program in C++
assert() example program in C++
Matrix using nested for loops in C++
Demonstration of Prefix and Postfix operators in C++
C++ Recursion function explained using Fibonacci series
Public versus Private members in C++
while (1) Loops in C++
C++ Destructors Versus Java Finalization
Most Emailed Articles (in C++)
A Brief History of C++
Difference between Procedural, Structured, and Object-Oriented Programming
C++ and Object-Oriented Programming
Getting Started with C++
Using cout in C++
Using Comments in a C++ Program
Using functions in C++
How to use Enumerated constants in C++
Demonstration of Prefix and Postfix operators in C++
The if Statement in C++
Advanced if Statements in C++
Use of Conditional (Ternary) Operator in C++
Demonstrating global and local variables in C++
Passing parameters to a function by value in C++
Returning values from a function in C++