0

C++challenge question

Came across this code today and trying to understand why this is not an error. https://code.sololearn.com/c1ad97CbAVrE/?ref=app The const int &b is passed a value during the function call, so being declared a const it should not change? However, during the a*=b operation it changes. Although it is declared as a const it is referencing the location of the value passed from the calling variable?

16th Aug 2020, 11:57 AM
GeoK68
GeoK68 - avatar
3 odpowiedzi
+ 2
a*=b will only change the value of a that is not const variable. so no error will be thrown by compiler bro. 🤗🤗
16th Aug 2020, 12:12 PM
BeastyDSK
BeastyDSK - avatar
+ 1
const int &b here it not making b constant. Values passed is stored in the locations of a, b from function call. Locations are already constants, so const int &b is same as int &b.. So you can't changing location value. Check this code as const int b; b++; //now this will be error.
16th Aug 2020, 12:08 PM
Jayakrishna 🇮🇳