Const reference in C++
Level 383 keystrokes
print takes a const string&, so it can read the argument without copying it and cannot modify it. It prints Hello and World.
C++ code
// const string& avoids copying the string and prevents the function from changing it1void print(const string& s) { // Reading is allowed; s = "x" here would be a compile error2 cout << s << endl;3}4 // A literal becomes a temporary string, which a const reference can bind to5print("Hello");// Prints World6print("World");Output
Hello World
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
"× 4<× 4;× 3(× 3)× 3{× 1}× 1&× 1