const
Concepts
Section titled “Concepts”- Const variables and structure objects
- Copying a const value into a non-const object
- Const references: reading, observing changes, and copying values
- Const function parameters: passing by value and by reference
- Reading through a pointer to const, reassigning the pointer, and observing changes
- Why copying an address does not remove the pointed-to
const - Const std::array elements
- Compile-time array sizes:
const intandconstexprstructures
Comprehension questions
Section titled “Comprehension questions”For each example, predict whether it compiles. If it does, what does it print? Which objects can be changed, and through which expressions?
Unless main is shown, place the snippet inside main. For all examples, include <array> and <iostream> before the code:
#include <array>#include <iostream>
int main(){ // snippet here}1. Reassigning a const variable
Section titled “1. Reassigning a const variable”const int a = 5;a = 6;std::cout << a << std::endl;Answer
The program will not compile: a = 6 tries to change a const object.
Initialization gives a its value, but subsequent assignments are forbidden.
Without the assignment, it prints 5.
2. Copying a const integer
Section titled “2. Copying a const integer”const int a = 5;int b = a;b = 6;std::cout << a << std::endl;std::cout << b << std::endl;Execution trace
Answer
It prints 5 and 6.
b is a separate, non-const object initialized with a copy of the value of a.
Reading a const object is allowed; changing the copy does not change the original.
3. A const structure object
Section titled “3. A const structure object”struct Position{ int x; int y;};
int main(){ const Position a{ 1, 2 }; a.x = 3; std::cout << a.x << std::endl;}Answer
The program will not compile: a.x = 3 tries to change a field of the const object a.
The fields are ordinary int fields; it is this particular Position object that is const.
Without the assignment, it prints 1.
4. Copying a const structure
Section titled “4. Copying a const structure”struct Position{ int x; int y;};
int main(){ const Position a{ 1, 2 }; Position b = a; b.x = 3; std::cout << a.x << std::endl; std::cout << b.x << std::endl;}Execution trace
Answer
It prints 1 and 3.
As with int, copying a const object into a non-const object is allowed.
The field values are copied into b; b.x is a different object from a.x.
5. Writing through a const reference
Section titled “5. Writing through a const reference”const int a = 5;const int& r = a;r = 6;std::cout << r << std::endl;Answer
The program will not compile: r = 6 tries to change a through a const reference.
r refers to the existing object a; it does not hold a copy of its value.
A const int& can refer to a const integer, but only reading through it is allowed.
Without the assignment, it prints 5.
6. Observing a change through a const reference
Section titled “6. Observing a change through a const reference”int a = 5;const int& r = a;std::cout << r << std::endl;a = 6;std::cout << r << std::endl;Execution trace
Answer
It prints 5 and 6.
A const reference can also refer to a non-const object.
a can still be changed directly, and r reads the current value of that same object.
The restriction applies to writes through r; it does not freeze a.
7. Copying a value read through a const reference
Section titled “7. Copying a value read through a const reference”int a = 5;const int& r = a;int b = r;b = 6;a = 7;std::cout << r << std::endl;std::cout << b << std::endl;Execution trace
Answer
It prints 7 and 6.
int b = r copies the value read through r into a separate, non-const object.
Changing b does not change a, and changing a does not change b.
The reference r continues to refer to a.
8. A non-const reference from a const reference
Section titled “8. A non-const reference from a const reference”int a = 5;const int& r = a;int& other = r;other = 6;Answer
The program will not compile at int& other = r.
A non-const reference cannot bind to the object through a const reference by discarding const.
Unlike int b = r, this would not create a separate integer: other would refer to the same object and allow writes to it.
The rule applies even though this particular a is non-const.
Use const int& other = r to keep the restriction; then other = 6 is also forbidden.
9. Structure fields through a const reference
Section titled “9. Structure fields through a const reference”struct Position{ int x; int y;};
int main(){ Position a{ 1, 2 }; const Position& r = a; r.x = 3; std::cout << r.x << std::endl;}Answer
The program will not compile: r.x = 3 tries to change an integer field through a const reference.
The same read-only access rule applies to a whole structure.
a.x = 3 would be allowed because a itself is non-const.
Without the assignment, it prints 1.
10. A const value parameter
Section titled “10. A const value parameter”void func(const int a){ a = 6;}
int main(){ int a = 5; func(a);}Answer
The program will not compile: the parameter a inside func is const.
The argument would be copied into this separate parameter object.
const on the parameter restricts changes to that copy; it does not make the caller’s variable const.
The same rule applies to a structure passed by value as const Position a.
11. A const argument and a non-const parameter
Section titled “11. A const argument and a non-const parameter”void func(int a){ a = 6; std::cout << a << std::endl;}
int main(){ const int a = 5; func(a); std::cout << a << std::endl;}Answer
It prints 6 and 5.
The const argument is copied into the ordinary int parameter.
Only that local copy changes, just as in the earlier integer-copy example.
12. Passing a const structure by value
Section titled “12. Passing a const structure by value”struct Position{ int x; int y;};
void func(Position a){ a.x = 3; std::cout << a.x << std::endl;}
int main(){ const Position a{ 1, 2 }; func(a); std::cout << a.x << std::endl;}Answer
It prints 3 and 1.
This combines structure copying with passing by value.
The parameter is a non-const copy, so changing its field is allowed and leaves the original unchanged.
13. A const reference parameter
Section titled “13. A const reference parameter”struct Position{ int x; int y;};
int readX(const Position& a){ return a.x;}
int main(){ Position a{ 1, 2 }; const Position b{ 3, 4 }; std::cout << readX(a) << std::endl; a.x = 5; std::cout << readX(a) << std::endl; std::cout << readX(b) << std::endl;}Answer
It prints 1, 5, and 3.
The references lab used increasePower(Arm&) to change the caller’s object.
Adding const keeps the reference to the original object but restricts access through the parameter to reading.
No Position object is copied when passing either argument.
Both const and non-const objects can be passed to const Position&.
The second call reads the updated a.x, while adding a.x = 6 inside readX would cause a compilation error.
A by-value parameter Position a would instead make a separate, mutable copy, as in the previous example.
14. A pointer to const
Section titled “14. A pointer to const”int a = 5;const int* p = &a;*p = 6;std::cout << *p << std::endl;Answer
The program will not compile: *p = 6 tries to write through a pointer to const.
const int* allows reading the pointed-to integer, but not changing it through p.
Without the assignment, it prints 5.
15. Reassigning a pointer to const
Section titled “15. Reassigning a pointer to const”int a = 5;int b = 6;const int* p = &a;p = &b;std::cout << *p << std::endl;Answer
It prints 6.
The object accessed through p is read-only through that pointer, but p itself can change.
Assigning &b changes the stored address; it does not overwrite either integer.
Where does const go?
const int* p and int const* p mean the same thing: a pointer to const int.
Here, const applies to the pointed-to object.
In int* const p = &a, const applies to the pointer variable instead:
p = &b is forbidden, but *p = 6 is allowed if a is non-const.
const int* const p = &a restricts both.
In this lab, the main focus is const T*: reading an object through a pointer.
16. Accessing structure fields through a pointer to const
Section titled “16. Accessing structure fields through a pointer to const”struct Position{ int x; int y;};
int main(){ Position a{ 1, 2 }; const Position* p = &a; p->x = 3; std::cout << p->x << std::endl;}Answer
The program will not compile: p->x = 3 attempts to change the object through const Position*.
Both integer fields can be read through p, but neither can be changed through it.
Without the assignment, it prints 1.
17. Observing a change through a pointer to const
Section titled “17. Observing a change through a pointer to const”struct Position{ int x; int y;};
int main(){ Position a{ 1, 2 }; const Position* p = &a; std::cout << p->x << std::endl; a.x = 3; std::cout << p->x << std::endl;}Answer
It prints 1 and 3.
a is still a non-const object and can be changed directly.
The pointer does not store a frozen copy of a; each read accesses the same object’s current field value.
const Position* restricts this access path, not every possible way of accessing a.
18. Copying an address does not copy the object
Section titled “18. Copying an address does not copy the object”struct Position{ int x; int y;};
int main(){ Position a{ 1, 2 }; const Position* p = &a; Position* q = p; q->x = 3;}Answer
The program will not compile at Position* q = p.
An ordinary pointer cannot be initialized from a pointer to const by discarding the pointed-to const.
Unlike Position b = a, copying a pointer copies an address, not the Position object.
Allowing this conversion would allow writes to the same object through q.
Use const Position* q = p to keep the restriction; then q->x = 3 is also forbidden.
This conversion is forbidden even though this particular a is non-const.
19. Copying the value read through a pointer to const
Section titled “19. Copying the value read through a pointer to const”struct Position{ int x; int y;};
int main(){ const Position a{ 1, 2 }; const Position* p = &a; Position b = *p; b.x = 3; std::cout << p->x << std::endl; std::cout << b.x << std::endl;}Answer
It prints 1 and 3.
Position b = *p reads and copies the whole object, rather than copying its address.
The new object b is non-const, so its fields can be changed.
This combines reading through a pointer with the earlier structure-copy rule.
20. A pointer-to-const parameter
Section titled “20. A pointer-to-const parameter”struct Position{ int x; int y;};
int readX(const Position* p){ return p->x;}
int main(){ Position a{ 1, 2 }; const Position b{ 3, 4 }; std::cout << readX(&a) << std::endl; std::cout << readX(&b) << std::endl;}Answer
It prints 1 and 3.
The function receives a copy of an address, not a copy of the structure.
It can read the original object but cannot change its integer fields through p.
Adding p->x = 5 inside readX would cause a compilation error.
Both non-const and const objects can be passed to a parameter of type const Position*.
The local pointer parameter itself can still be reassigned, as in the earlier pointer example.
21. Returning a non-const pointer to a field
Section titled “21. Returning a non-const pointer to a field”struct Position{ int x; int y;};
int* addressOfX(const Position& a){ return &a.x;}
int main(){ Position a{ 1, 2 }; int* p = addressOfX(a); *p = 5; std::cout << a.x << std::endl;}Answer
The program will not compile at return &a.x.
Through a const reference, the field a.x is also const, so &a.x has type const int*.
Converting it to int* would discard const, just like binding int& to a const reference.
If this were allowed, the caller could change the object through the returned pointer,
bypassing the restriction of the parameter.
Returning const int* compiles, but then *p = 5 is forbidden.
22. Returning a pointer stored in a field
Section titled “22. Returning a pointer stored in a field”struct Holder{ int* p;};
int* storedPointer(const Holder& h){ return h.p;}
int main(){ int value = 1; Holder holder{ &value }; int* p = storedPointer(holder); *p = 5; std::cout << value << std::endl;}Answer
It prints 5.
Through the const reference, the field h.p is const: h.p = nullptr would not compile.
But the field is the pointer itself, not the integer it points to.
Its type is int* const, so returning it makes a copy of the address of type int*,
and copying a const value is allowed.
The integer value is not part of the structure, so const does not protect it.
Const std::array
Section titled “Const std::array”1. A const std::array
Section titled “1. A const std::array”const std::array<int, 2> arr{ 1, 2 };arr[0] = 3;std::cout << arr[0] << std::endl;Answer
The program will not compile: the elements of arr are const integers.
They can be read, but arr[0] = 3 attempts to change one of them.
Without the assignment, it prints 1.
2. Copying a const C++ array
Section titled “2. Copying a const C++ array”const std::array<int, 2> arr{ 1, 2 };std::array<int, 2> other = arr;other[0] = 3;std::cout << arr[0] << std::endl;std::cout << other[0] << std::endl;Execution trace
Answer
It prints 1 and 3.
The whole array is copied into the non-const object other.
Just as when copying a const structure, the copy can be changed independently of the original.
3. data from a const C++ array
Section titled “3. data from a const C++ array”const std::array<int, 2> arr{ 1, 2 };int* p = arr.data();Answer
The program will not compile: for this const array, arr.data() returns const int*.
Initializing int* from it would discard the pointed-to const.
Use const int* p = arr.data() to read the elements; writing through that pointer remains forbidden.
4. Reading an array through a pointer to const
Section titled “4. Reading an array through a pointer to const”std::array<int, 2> arr{ 1, 2 };const int* p = arr.data();arr[0] = 3;std::cout << *p << std::endl;p += 1;std::cout << *p << std::endl;Answer
It prints 3 and 2.
The pointer receives the address of the array’s first element.
*p reads the current value of arr[0], which was changed directly.
The pointer can advance to the next element, but *p = 4 is not allowed.
This combines array access and pointer arithmetic with read-only access through a pointer.
5. Const integers as array sizes
Section titled “5. Const integers as array sizes”const int globalSize = 2;
int main(){ const int localSize = 3; std::array<int, globalSize> a{ 1, 2 }; std::array<int, localSize> b{ 3, 4, 5 }; std::cout << a[1] << std::endl; std::cout << b[2] << std::endl;}Answer
It prints 2 and 5.
Both sizes are const integers initialized with constant expressions, so their values can be used at compile time.
Being global is not required: the local localSize works too.
Here, “size known at compile time” describes the size template argument, not the static storage keyword.
6. Const does not always mean known at compile time
Section titled “6. Const does not always mean known at compile time”int computeSize(){ int width = 2; int height = 3; return width * height;}
int main(){ const int size = computeSize(); std::array<int, size> arr{};}Answer
In standard C++, the program will not compile at std::array<int, size> arr{}.
size cannot be reassigned, but its initializer calls a function that is not declared constexpr.
That call is not a constant expression, even if the compiler can optimize it to a fixed value.
The size template argument of std::array must be a constant expression.
7. A const structure and an array size
Section titled “7. A const structure and an array size”struct Sizes{ int count;};
int main(){ const Sizes sizes{ 3 }; std::array<int, sizes.count> arr{ 1, 2, 3 }; std::cout << arr[2] << std::endl;}Answer
The program will not compile: sizes.count is not a constant expression usable as an array size.
const prevents changes to sizes, but does not make its fields usable in constant expressions.
The rule that allows const int localSize = 3 as an array size does not extend to a const structure.
8. A constexpr structure and an array size
Section titled “8. A constexpr structure and an array size”struct Sizes{ int count;};
int main(){ constexpr Sizes sizes{ 3 }; std::array<int, sizes.count> arr{ 1, 2, 3 }; std::cout << arr[2] << std::endl;}Answer
It prints 3.
A constexpr variable must be initialized with a constant expression and is also const.
Here, the whole Sizes object is initialized at compile time, so reading sizes.count is valid as a size template argument.
For the earlier const int localSize = 3, constexpr was not needed to use the value as a bound.
For this structure, writing only const Sizes sizes{ 3 } would not make sizes.count a constant expression usable as a bound.
constexpr explicitly provides that guarantee for this object.
This extends the literal size arguments introduced in the C++ array lab.