Defining constants

In some cases, you will want to provide constant values that can be used throughout your code. For example, you may decide to declare a constant for π. You should not allow this value to be changed because it will change the underlying logic in your code. This means that you should mark the variable as being constant. When you do this, the compiler will check the use of the variable and if it is used in code that changes the value of the variable the compiler will issue an error:

    const double pi = 3.1415; 
double radius = 5.0;
double circumference = 2 * pi * radius;

In this case the symbol pi is declared as being constant, so it cannot change. If you subsequently decide to change the constant, the compiler will issue an error:

    // add more precision, generates error C3892 
pi += 0.00009265359;

Once you have declared a constant, you can be assured that the compiler will make sure it remains so. You can assign a constant with an expression as follows:

    #include <cmath> 
const double sqrtOf2 = std::sqrt(2);

In this code, a global constant called sqrtOf2 is declared and assigned with a value using the std::sqrt function. Since this constant is declared outside a function, it is global to the file and can be used throughout the file.

The problem with this approach is that the preprocessor does a simple replacement. With constants declared with const, the C++ compiler will perform type checking to ensure that the constant is being used appropriately.

You can also use const to declare a constant that will be used as a constant expression. For example, you can declare an array using the square bracket syntax (more details will be given in Chapter 2, Working with Memory, Arrays, and Pointers):

    int values[5];

This declares an array of five integers on the stack and these items are accessed through the values array variable. The 5 here is a constant expression. When you declare an array on the stack, you have to provide the compiler with a constant expression so it knows how much memory to allocate and this means the size of the array must be known at compile time. (You can allocate an array with a size known only at runtime, but this requires dynamic memory allocation, explained in Chapter 2, Working with Memory, Arrays, and Pointers.) In C++, you can declare a constant to do the following:

    const int size = 5;  
int values[size];

Elsewhere in your code, when you access the values array, you can use the size constant to make sure that you do not access items past the end of the array. Since the size variable is declared in just one place, if you need to change the size of the array at a later stage, you have just one place to make this change. The const keyword can also be used on pointers and references (see Chapter 2, Working with Memory, Arrays, and Pointers) and on objects (see Chapter 4, Classes); often, you'll see it used on parameters to functions (see Chapter 3, Using Functions). This is used to get the compiler to help ensure that pointers, references, and objects are used appropriately, as you intended.