Mooophy / Cpp-Primer

C++ Primer 5 answers
Creative Commons Zero v1.0 Universal
8.06k stars 3k forks source link

&r = 0; is legal? #842

Closed itskemo closed 3 months ago

itskemo commented 3 months ago

https://github.com/Mooophy/Cpp-Primer/blob/abcb44b77668abaca539c482d359d17f5a0e3d7f/ch02/README.md?plain=1#L415C1-L415C26

int i = -1, &r = 0;         // illegal, r must refer to an object.
const int i = -1, &r = 0; // legal.

How come &r = 0; is legal on the second line but the line above it is illegal?

I believe this is incorrect because in C++, a reference must be initialized to refer to an already existing object, not a literal value like 0? 🙏