0
int *pointer = nullptr;
const int *ponter2 = pointer; // working
const int *&referpointer = pointer; // error 
const int *& const referpointer2 = pointer; // it is working;

I wonder why a plain pointer can initialize a reference to a constant pointer to a constant, but cannot initialize a reference to a pointer to a constant

AndyG
  • 39,700
  • 8
  • 109
  • 143
chey
  • 11
  • 3

1 Answers1

-1

If it did work, you could do this

int i;
int *pointer = &i;
const int *&referpointer = pointer;

const int const_i = 4;
const int *const_pointer = &const_i;
referpointer = const_pointer; // same as pointer=const_pointer; via the reference
*pointer = 5; // changes const_i

Then you would have found a way to change a const variable.

That's why const int *&referpointer = pointer; is not allowed.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • This is not changing a const variable. `int i` is not declared as `const`, and so it is valid to change. The pointer types you've declared are also not `const` either, but merely pointers to const or references of pointers to const. – AndyG Nov 30 '22 at 15:06
  • @AndyG it changes `const_i` – user253751 Nov 30 '22 at 15:06
  • While we're pretending this fictional scenario would compile, it would not change `const_i` because `pointer` is only referencing non-const `int i`, and so dereferencing and assigning does not change any const variables. The syntax is simply invalid because of qualifier mismatches. – AndyG Nov 30 '22 at 15:09
  • @AndyG what do you think `referpointer = const_pointer` does? – user253751 Nov 30 '22 at 15:10
  • It is always possible I'm misunderstanding what your hypothetical code is trying to demonstrate, however my interpretation is that the expression `referpointer = const_pointer` is an attempt to reassign a reference to a pointer to const int from referencing `pointer` to reference `const_pointer`. The subsequent assignment of `*pointer = 5` appears to be a non-sequitur. – AndyG Nov 30 '22 at 15:17
  • @AndyG References cannot be reassigned. If you know C++ then you know this. "Assigning a reference" assigns the object which the reference refers to. – user253751 Nov 30 '22 at 15:18
  • Lol I'm well aware that references cannot be reassigned, but we're living in a hypothetical world where your code is valid. Anything can go. I think your post could use more exposition to explain exactly what it's trying to achieve. I think I now understand that you're ultimately trying to say that we end up in a situation where `pointer` and `const_pointer` are both pointing to `const_i`, and subsequently `*pointer = 5` would then modify `const_i`. – AndyG Nov 30 '22 at 15:22
  • @AndyG according to your logic proof-by-contradiction is not a valid kind of argument. we can say "1+1 isn't 3 because if it was 3 then 1+1 would be the same as 1+1+1." and you would say "yes but in that hypothetical world anything goes - there would be demons flying out of my nose zapping me with space lasers therefore the argument is invalid" – user253751 Nov 30 '22 at 15:37
  • It's simpler than that; I just didn't quite understand what you were trying to convey – AndyG Dec 01 '22 at 00:42