CHAPTER 23

image

Constants

A constant is a variable that has a value which cannot be changed once the constant has been assigned.

Constant variables

A variable can be made into a constant by adding the const keyword either before or after the data type. This modifier means that the variable becomes read-only, and it must therefore be assigned a value at the same time as it is declared. Attempting to change the value will result in a compile-time error.

const int var = 5;int const var2 = 10; // alternative order

Constant pointers

When it comes to pointers, const can be used in two ways. First, the pointer can be made constant, which means that it ...

Get C++ Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.