- 01Getting Started
- 02Your First Output (cout)
- 03Variables and Types (int/double/string/bool)
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Pointer Basics
- 10References
- 11Classes (Object-Oriented Programming)
- 12Constructors and Destructors
- 13Inheritance
- 14Using vector (the Standard Library)
- 15String Manipulation (string)
- 16Structs (struct)
- 17Error Handling (try/catch)
- 18The Ternary Operator
- 19The switch Statement
- 20[Applied] Build a Mini Inventory Management System
References
This lesson covers references, a safer way than pointers to handle an "alias" for a variable. It's for anyone searching "C++ reference usage."
A reference gives an existing variable a second name. Declare one in the form type& variableName = originalVariable;, and unlike a pointer, you can treat it as if you're operating on the original variable directly, without writing * or & every time.
The example uses a reference, int& value, as a function parameter, so changing the value inside the function also affects the caller's variable.
A common early mistake is not seeing the difference from a pointer. A reference can't be re-bound to a different variable once linked, and it also can't have a "points to nothing" state like nullptr. That makes it usable more safely than a pointer.
In real projects, references are used a lot when passing large data to a function โ avoiding the cost of copying it, while still being able to modify the value.
๐งช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
