Open utterances-bot opened 3 years ago
Forget about 4., the pointer chapter already contains it.
This compiles just fine with g++ (Windows)
#include <iostream>
int main()
{
int i = 2;
int k = 3;
int& ri = i; // reference to i
ri = k; // COMPILER ERROR: reference can't be redirected
std::cout << i; // 3
// int& r2; // COMPILER ERROR: reference must be initialized
// double& r3 = i; // COMPILER ERROR: types must agree
}
I'm referring to the box at the top of this page, that says that a reference can't be redirected (but here I'm just assigning k
value and not redirecting to k
address right?).
@mg979: you're absolutely right - I don't know what I was thinking when I wrote this. One more reason I plan to make all code boxes runnable in Compiler Explorer. As of now, only the standard library algorithm examples are all executable in the browser.
String literals are lvalues, we can get address of them like &"Hello World!"
.
In paragraph "Usage/ Const reference parameters" a closing parenthesis is missing on this line: if (y.empty() return x;
C++ References Introduction | hacking C++
C++ references for beginners - properties, usage, guidelines, pitfalls: declaration, properties, const references, usage as function parameters, dangling, binding rules, etc.
https://hackingcpp.com/cpp/lang/references.html