The Reserved Word const in C++
C++ Just After the Basics - Part 8
Forward: In C++, the reserved word, “const”, depending on where it used, makes different things constant. I explain all that in this article.
By: Chrysanthus Date Published: 22 Aug 2012
Introduction
You need basic knowledge in C++ in order to understand this article. If you do not have that knowledge, then read the series whose first part is titled, “Getting Started with C++” in this blog. To arrive at the series, type the title and my name, Chrys, in the Search Box of this page and click Search.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Object in C++
An object in C++ is a region in memory, which can hold a datum (singular for data).
Identifier
Any object in memory can be identified by a word, called the identifier. You the programmer choose a word of your choice for the identifier.
Pointer
When talking about a pointer in C++, two objects are involved: the pointed object and the pointer object. The pointed object is the object of interest; it can have an identifier. The pointer object has the memory address of the pointed object. The datum for the pointer object is this memory address. The pointer object normally has its identifier. Do not confuse between the pointer object and the pointed object.
Class
A class is a set of object identifiers and functions related to the identifiers. The identified objects are called data members or properties. The functions are called member functions or methods. When the properties are given values, the class becomes what is known as an instantiated object. Since different values (data) can be given to the properties, so different instantiated objects can be made from one class.
Consider the following program (which will not compile):
#include <iostream>
using namespace std;
int main()
{
const int myInt = 5;
myInt = 7;
return 0;
}
The first statement in main makes the value of the object identified by myInt constant. Note the position of the word, const in the left operand of the initialization statement. The program will not compile because the second statement in main tries to change the constant value. If you take off the second statement, the code will compile.
Constant Pointer and Constant Pointed Value
We look at the case involving pointers here. In the following program (which will not compile), the value of the pointed object is made constant in the pointer declaration:
#include <iostream>
using namespace std;
int main()
{
int myInt = 5;
const int *intPtr = &myInt;
*intPtr = 7;
return 0;
}
The first statement in main creates an int object with value 5. The second statement makes this value constant so far as the pointer is concerned. Note the position of the word, const, in the pointer declaration. You cannot change the value (5) of the pointed object using the pointer. The above program does not compile because the third statement in main tries to change the pointed object value through the pointer. If you remove this statement, the program will compile. However, you can change the value of the pointed object using the pointed object’s identifier, as in the following code (which compiles):
using namespace std;
int main()
{
int myInt = 5;
const int *intPtr = &myInt;
myInt = 7;
return 0;
}
In the following program (which will not compile), the value of the pointer object is made constant in the pointer declaration:
#include <iostream>
using namespace std;
int main()
{
int myInt = 5;
int herInt = 6;
int *const intPtr = &myInt;
intPtr = &herInt;
return 0;
}
The first two statements in main create two objects with different values. The memory address of the first object is &myInt and the memory address of the second object is &herInt. The third statement makes the value (address) of the pointer object constant. The pointed object here is the object (myInt) in the first statement. Note the position of the word, const, relative to the asterisk in the third statement. Here, we are not interested in making the value of the pointed object constant, so it does not matter whether the value of the pointed object is constant. The above program does not compile because the fourth statement in main, tries to change the pointer object value by assigning the address of the second object (from second statement). If you remove this statement, the program will compile.
You can make both the values of the pointer object and the pointed object constant, through the pointer. The syntax is:
const Type *const pointer = &pointed;
Here, the word, const, is used twice. Note their positions.
In a class, you can forbid a method definition from changing the value of properties using the word, const. Note that in this case, the method and property belong to the same class (object).
The following code will not compile because the method definition tries to change (or give) the value of the property:
#include <iostream>
using namespace std;
class CC
{
int prop1;
void mthd() const
{
prop1 = 3;
}
};
int main()
{
return 0;
}
Note the position of the word, const, in the method declaration. The word is on the right of the declaration. By this position, the method block cannot change the value of the property. In C++ specifications, you will see this kind of method declaration (prototype), standing on its own, like,
void CC::mthd() const;
You may think this kind of constancy is not necessary. In C++, some member functions (methods) can be overloaded (see later), so the first writer of the program uses this constancy to indicate to the next one (who updates the program) that if the function is overloaded, it should not change the value of any property in the class (object).
This is a summary on how to interpret what is constant in C++.
- You can decide to make the value (datum) of an object constant so far as the identifier is concerned.
- In the case of pointers, you can decide to make the value of the pointed object constant or the value of the pointer object constant or both. Here, you cannot change the value of the pointed object using the pointer (address), but you can change it using the pointed object’s identifier.
- In the case of a class, you can prevent a method from changing the values of the properties of the class. In this case the declaration is constant in the sense that it does not have the authority to change the value of any of the properties of the instantiated object to which it belongs.
Comment on const in Function Parameter
Consider the following function prototype:
void fn(const int ID);
The word, const, here is in the light of constant value. Here the value of ID in the function definition (block) cannot be changed. The word, const, here has the same effect as it has when it is in front of a non-pointer identifier declaration statement (see above). The following code will not compile because the function definition tries to change the value of the parameter, from the one sent as argument (5) in the function call:
#include <iostream>
using namespace std;
void fn(const int ID)
{
ID = 3;
}
int main()
{
fn(5);
return 0;
}
The following code will compile as no change is done to the parameter value:
#include <iostream>
using namespace std;
void fn(const int ID)
{
int answer = ID + 4;
cout << answer;
}
int main()
{
fn(5);
return 0;
}
That is what I prepared for The Reserved Word const in C++. Hope you like it. We continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT