コードスニペットに出くわしました
const int& reference_to_const_int = 20;
cout<<"\n reference_to_const_int = "<<reference_to_const_int<<endl;
このコードはコンパイルされ、出力とともに実行されます:-
reference_to_const_int = 20
これは私にとって奇妙なことです。私が知っているように、参照はメモリを占有せず、他の変数のエイリアスです。したがって、私たちは言うことができません
int& reference_to_int = 30;
上記のステートメントはコンパイルせず、エラーが発生します:-
error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
「constint&」の場合、正確には何が起こっているのでしょうか。完全な説明が望まれます。
親切に助けてください。
ありがとう