Search⌘ K

Const

Explore how the const keyword in C++ can be used to create variables that cannot be modified, understand different pointer constness, and learn how to write class methods that guarantee immutability. This lesson helps you write safer and more efficient C++ code by mastering const usage.

Definition

The const keyword affects the behavior of a variable. Any variable initialized with the const keyword cannot be modified later on. It is constant.

const int i = 10;
i = 20; // "Error: Cannot modify a constant variable"

Variables or attributes of ...