The `const` Keyword in C++

Language:中文/EN

The Role of the const Keyword

In programming, we often need to define a variable whose value remains constant, such as defining pi=3.14, e=2.72, or the elastic modulus of a material. In these cases, the const keyword is needed.

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

Additionally, in C++ programs that consist of multiple files, you might need to define a const variable in one file and declare and use it in another. In such cases, you need to use the extern keyword during both declaration and definition.

// Declare and define a const variable
extern const int buffSize=100;
// When using this variable in other files, declare it
extern const int buffSize;
------------------------------------
// Note that the extern keyword is a means to separate variable declaration and definition
// A variable can and should only be defined once, but can be declared multiple times

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

extern int i=100;
// This will cause an error, as any declaration with explicit initialization is also a definition, negating the effect of extern

extern const int buffSize=100;
// The above statement can be seen as a special case, where declaration and definition occur simultaneously

References to const

In C++, a reference can be seen as an alias for an object. A reference to const is often called a constant reference. This type of reference can be bound to both constant and non-constant objects, but a non-const reference cannot be bound 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 modify the value of the non-constant object through this constant reference.

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 differ from references in that references are not objects, but pointers themselves are objects. Therefore, you can define pointers to constants, constant pointers, and constant pointers to constants. When the pointer itself is a constant, it is called a top-level const, and when the pointer points to a constant, it is called a low-level const.

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

For low-level const, i.e., pointers to constants, like constant references, there is no requirement that the pointed-to object must be a constant object. However, when the pointed-to object is not a constant, you cannot modify the value of this non-constant object through the pointer, but you can modify it through other means.

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

For top-level const, i.e., a constant pointer, the value of the pointer itself cannot be changed. For example, you cannot change the pointer’s value to index array elements, but if it points to a non-constant object, modifying the value of the pointed-to object through the pointer is entirely permissible.

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