How to Use the Keyword const in C++

By Stephen R. Davis

A pointer is a variable that “points at” other variables in C++. The keyword const means that a variable cannot be changed once it has been declared and initialized.

const double PI = 3.1415926535;

Arguments to functions can also be declared const, meaning that the argument cannot be changed within the function. However, this introduces an interesting dichotomy in the case of pointer variables. Consider the following declaration:

const int* pInt;

Exactly what is the constant here? What can you not change? Is it the variable pInt or the integer pointed at by pInt? It turns out that both are possible, but this declaration declares a variable pointer to a constant memory location. Thus the following:

const int* pInt;   // declare a pointer to a const int
int nVar;
pInt = &nVar;      // this is allowed
*pInt = 10;        // but this is not

You can change the value of pInt, for example, assigning it the address of nVar. But the final assignment in the example snippet generates a compiler error since you cannot change the const int pointed at by pInt.

What if you had intended to create a pointer variable with a constant value? The following snippet shows this in action:

int nVar;
int * const cpInt = &nVar; // declare a constant pointer
                           // to a variable integer
*cpInt = 10;               // now this is legal...
cpInt++;                   // ...but this is not

The variable cpInt is a constant pointer to a variable int. The programmer cannot change the value of the pointer, but she can change the value of the integer pointed at.

The const-ness can be added via an assignment or initialization but cannot be (readily) cast away. Thus, the following:

int nVar = 10;
int pVar = &nVar;
const int* pcVar = pVar;   // this is legal
int* pVar2 = pcVar;        // this is not

The assignment pcVar = pVar; is okay — this is adding the const restriction. The final assignment in the snippet is not allowed since it attempts to remove the const-ness restriction of pcVar.

A variable can be implicitly recast as part of a function call, as in the following example:

void fn(const int& nVar);
void mainFn()
    int n;
    fn(10);  // calls fn(const int&)
    fn(n);   // calls the same function by treating n
}            // as if it were const

The declaration fn(const int&) says that the function fn() does not modify the value of its argument. That’s important when passing a reference to the constant 10. It isn’t important when passing a reference to the variable n, but it doesn’t hurt anything either.

Finally, const can be used as a discriminator between functions of the same name:

void fn(const int& nVar);
void fn(int& nVar);
void mainFn()
    int n;
    fn(10);  // calls the first function
    fn(n);   // calls the second function