Lesson 6 of 45 · C++
Lesson 14: References
Duration: 8 min
Lesson 6 of 45 · C++
Duration: 8 min
cpp\nint x = 10;\nint& ref = x; // ref is another name for x\nref = 20; // x becomes 20\nstd::cout << \"x = \" << x << '\\n';\n\n// Const reference allows read‑only access without copying\nconst std::string& cs = std::string(\"immutable\");\nstd::cout << cs << '\\n';\n\n\nReferences are required for operator overloading (e.g., operator=) and for range‑based for loops when iterating by reference to avoid copies.\n\n> Tip: Use const& when you need to pass large objects without modification.\n\n---\n\n