Constant reference can be changed? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Constant reference can be changed?

Hello Output of below code is 1111 , but I was expecting compile error : int a =10; int const &b = a; a = 11; cout << a << b; If we refer second line, b is constant and it is just an alliance of variable a... now, third line is trying to change value of a (inturn variable b). how come value is printed instead of compile error.

31st May 2018, 9:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Antwort
+ 2
b refers to a, but a is not const so you can still change it. But you can't assign anything using b, it's read-only.
1st Jun 2018, 1:48 PM
Arc
Arc - avatar