Itβs hard for me to make sense of the const int* const &alias_for_ptr = ptr;
following:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptr = &a;
const int* const &alias_for_ptr = ptr;
ptr = NULL;
if (ptr == alias_for_ptr)
cout << "ptr == alias_for_ptr" << endl;
else
cout << "ptr != alias_for_ptr" << endl;
return 0;
}
Why ptr == alias_for_ptrreturns false (actually alias_for_ptrsaves the old value &a)? I got the impression that alias_for_ptrit will always have the same meaning as ptr(although the use of the symbol and symbol), and that const int* const X = Yonly guarantees that I can not change both the value Xand the specified value Xthrough this identifier X.
Also, if I delete the second const, then the script will not compile, which confuses me further. Note that the compiler error: invalid initialization of reference of type βconst int*&β from expression of type βint*β.