C++ Constants

Introduction

When we want to have a read-only object or promise not to change the value of some object in the current scope, we make it a constant.

C++ uses the const type qualifier to mark the object as a read-only.

We say that our object is now immutable.

To define an integer constant with a value of 5, for example, we would write:

int main() 
{ 
    const int n = 5; 
} 

We can now use that constant in places such as an array size:

int main() 
{ 
    const int n = 5; 
    int arr[n] = { 10, 20, 30, 40, 50 }; 
} 

Constants are not modifiable, attempt to do so results in a compile-time error:

int main() 
{ 
    const int n = 5; 
    n++; // error, can't modify a read-only object 
} 

An object declared const cannot be assigned to; it needs to be initialized.

So, we can't have:

int main() 
{ 
    const int n;       // error, no initializer 
    const int m = 123; // OK 
} 

const modifies an entire type, not just the object.

So, const int and int are two different types.

The first one is said to be const-qualified.




PreviousNext

Related