Skip to content

The many jobs of const in C++

Question

Describe the different ways the const keyword is used in C++.

Short answer

It marks things the code promises not to modify. Uses include constant variables; pointers, where const T* protects the pointee and T* const fixes the pointer; const T& parameters for cheap read-only passing; const member functions that cannot change the object (except mutable members) and are the only ones callable on const objects; and const return values. The compiler enforces it, catching bugs and documenting intent.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.31, p.233; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question