New course! Every coder should learn Generative AI!
Try a free lesson0
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.
1 Answer
+ 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.