Constant variables

What is a constant variable?

A constant variable is a variable that once defined, can not have its value changed. It is recommended to always define variables as constant when their content never change as it prevent changing them by mistake.

Defining constant variables

To define a variable as constant, we must add the keyword const a the beginning of its definition.

Example:

const double PI = 3.1415926;

It makes sense to define a variable storing the value of pi as a constant since the value of pi will always stay be the same. Because the variable PI is constant, if we write the following:

PI = 10;

The code would not compile as it would produce an error.

Note that it is also possible to put the const keyword after the type:

double const PI = 3.1415926;

It is, however, rarely done.

Constant references

We can define a reference as a constant too. With a constant reference, we can not change the value of the variable given as argument. Like with a normal argument variable, it ensures that the value of the variable given in argument will not be changed, but it has the advantage that there is no copy of the variable made (No new variable is created).

To define a reference as constant, we simply have to add the keyword const before its type. Example:

void function(const int & arg) { std::cout << arg << std::endl; }

In the function defined above, we can use the variable arg to retrieve value of the variable given as argument, but we can not try to change it, otherwise it would cause an error.

Note that we can also add the const keyword after the type of the reference:

void function(int const & arg) { std::cout << arg << std::endl; }

Unlike non-constant references, constant references may refer to values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> void func(int & arg) { std::cout << arg << std::endl; } void func2(const int & arg) { std::cout << arg << std::endl; } int main() { func(123); // Error: Non-constant references may not refer to values. func2(123); // It works, the reference is constant. return 0; }

Non-constant references may change the value of the variables they refer to. They can not refer to values, because changing the value of a value does not make sense.

Functions returning constant references

We can make a function return a constant reference by adding the keyword const, in the definition of the function, before or directly after the name of the return type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> const int& func(int& arg) { arg+=5; return arg; } int main() { int var1 = 5, var2 = 0; // 'var2' is assigned with the value of 'var1', which has been added with 5, by the function. var2 = func(var1); // Prints the value of 'var1' and 'var2'. std::cout << var1 << std::endl; std::cout << var2 << std::endl; return 0; }

By making the returned reference constant, we ensure that the value of the referenced variable will not be changed. Since the function func, in the example above, returns a constant reference, we could not write the following, as it would cause a compilation error:

func(var1) = 6;

If the reference was not constant, we could have.

We could also not write the following (Because the pointer do not point to a constant variable):

int* ptr = &func(var1);

Note that adding the keyword const after the type is also valid:

int const& func(int& arg) { arg+=5; return arg; }