The const Keyword in C++

What const Does

When programming we often need to define a kind of variable whose value never changes, for example pi=3.14, e=2.72, or the elastic modulus of a material. That is when the const keyword comes in.

const pi=3.14;
const e=2.72;
const E=2.3;

There is also another need: because a C++ program often consists of multiple files, sometimes you need to define a const variable in one file and declare and use it in others. In that case you add the extern keyword to both the declaration and the definition.

//Declare and define a const variable
extern const int buffSize=100;
//To use that variable in another file, you must declare it
extern const int buffSize;
------------------------------------
//Note that the extern keyword is a means of separating a variable's declaration from its definition.
//A variable can and must be defined exactly once, but it can be declared multiple times.

extern int i;	//Declare a variable without defining it
int i;	//Declare and define

extern int i=100;
//This causes an error: any declaration that includes an explicit initializer also becomes a definition, canceling out the effect of extern

extern const int buffSize=100;
//The statement above can be seen as a special case that declares and defines at the same time

References to const

A reference in C++ can be thought of as an alias for an object. A reference to const is also commonly called a constant reference. Such a reference can be bound either to a constant object or to a non-constant object, but you cannot bind a non-const reference to a constant object.

const int ci=1024;
const int &r1=ci;	//Bind a constant reference to a constant object
r1=42;
int &r2=ci;		//Error: binding a non-constant reference to a constant object

Binding a constant reference to a non-constant object is allowed, but you cannot use that constant reference to modify the value of the non-constant object.

int i=42;
const int &r=i;
r=0;	//Error: using a constant reference to change the value of a non-constant object

const and Pointers

Pointers are different from references. A reference is not an object, but a pointer itself is also a kind of object. Therefore you can define a pointer to const and a const pointer, and of course a const pointer to const as well. When the pointer itself is a constant, this is called a top-level const; when the pointer points to a constant, it is called a low-level const.

int errNumb=0;
int *const curErr=&errNumb;	//Define a const pointer to a non-constant object
const double pi=3.14;
const double *const pip=π	//Define a const pointer to a constant object

For a low-level const, that is, a pointer to const, just like a constant reference, the object pointed to is not required to be a constant object. But when the object pointed to is not a constant object, you cannot use this pointer to modify the value of that non-constant object, although you can modify the object’s value by other means.

int errNumb=0;
const int *curErr=&errNumb;	//A const pointer to a non-constant object

For a top-level const, that is, a const pointer, the pointer’s own value cannot change; for example, you cannot change the pointer’s value to index array elements. But if it points to a non-constant object, using the pointer to modify the value of the object it points to is entirely allowed.

int errNumb=0;
int *const curErr=&errNumb;
*curErr=0;	//Modifying the value of the pointed-to object through a const pointer is allowed