Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The const qualifier

Reference for C++ const: const variables, pointer to const versus const pointer, const references, const member functions, top-level const and const_cast.

const marks something as read-only through a given name. A const variable cannot be assigned to after initialization, a reference-to-const cannot change what it refers to, and a const member function promises not to modify the object it is called on. The compiler enforces these promises and may use them to optimize.

Using const wherever it is true is called const correctness. It documents intent, catches accidental writes, and lets functions accept temporaries and read-only data.

C++
const int limit = 100;          // read-only variable
const std::string& name = s;    // reference to const
int get() const;                // const member function

Try it

C++ const with pointers and references

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ const member functions and overloading on const

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
const T x = value;          T const x = value;      // same thing
const T* p;                 T const* p;             // pointer to const T
T* const p = &x;                                    // const pointer to T
const T& r = x;                                     // reference to const T
R method(params) const;                             // const member function

Read declarations right to left: in int const* const p, p is a const pointer to a const int.

Where Effect
Variable Must be initialized; later assignment is an error. At namespace scope it has internal linkage by default (unlike a non-const variable).
Pointer target const T* forbids writing through the pointer.
Pointer itself T* const forbids re-pointing.
Member function this becomes const C*. The function may be called on const objects and cannot modify non-mutable data members.
Function parameter A top-level const on a by-value parameter does not change the function's type; it only restricts the body.

Usage notes

  • Pass large read-only arguments as const T&; pass small trivially copyable ones (int, double, std::string_view) by value.
  • const is shallow: a const object holding a pointer cannot re-point it but can still modify the pointed-to data. Member T* becomes T* const, not const T*.
  • Overloading on const is how containers provide both operator[] returning T& and const T&.
  • Returning a const value (const int f()) is pointless and can block moves for class types. Return const T& only when it refers to something that outlives the call.
  • Modifying an object that was defined const is undefined behavior, even through const_cast. const_cast is only valid when the object itself is not const, as with a legacy API that forgot const.
  • Const objects of a class type with no user-provided default constructor must be initialized explicitly: const Label l; works only if the class has a user-provided or defaulted-with-initializers constructor.
  • Prefer constexpr for values that are needed at compile time, and const for runtime read-only values.
  • const and volatile are called cv-qualifiers.