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.
const int limit = 100; // read-only variable const std::string& name = s; // reference to const int get() const; // const member function
Try it
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.
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
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. constis shallow: aconstobject holding a pointer cannot re-point it but can still modify the pointed-to data. MemberT*becomesT* const, notconst T*.- Overloading on
constis how containers provide bothoperator[]returningT&andconst T&. - Returning a const value (
const int f()) is pointless and can block moves for class types. Returnconst T&only when it refers to something that outlives the call. - Modifying an object that was defined
constis undefined behavior, even throughconst_cast.const_castis only valid when the object itself is not const, as with a legacy API that forgotconst. - 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
constexprfor values that are needed at compile time, andconstfor runtime read-only values. constandvolatileare called cv-qualifiers.