C++ References: Pass by Reference & const Reference Guide
Module 9: References in C++
A reference is an alias (another name) for an existing variable. Unlike pointers, references must be initialized when they are created and cannot be changed to refer to another variable.
References make code simpler, safer, and more efficient, especially when passing large objects to functions.
1. Reference Variables
A reference variable refers to an existing variable using the & symbol.
Syntax
1dataType& referenceName = variableName;
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 int number = 10; 7 int& ref = number; 8 9 cout << "Number: " << number << endl; 10 cout << "Reference: " << ref << endl; 11 12 ref = 20; 13 14 cout << "Updated Number: " << number; 15 16 return 0; 17}
Output
1Number: 10 2Reference: 10 3Updated Number: 20
Changing the reference also changes the original variable.
2. Pass by Reference
Passing variables by reference allows a function to modify the original value without creating a copy.
Example
1#include <iostream> 2using namespace std; 3 4void increment(int& value) 5{ 6 value++; 7} 8 9int main() 10{ 11 int number = 10; 12 13 increment(number); 14 15 cout << number; 16 17 return 0; 18}
Output
111
Advantages
- No unnecessary copying of data.
- Faster for large objects.
- Changes affect the original variable.
3. Return Reference
A function can return a reference to an existing variable.
Example
1#include <iostream> 2using namespace std; 3 4int value = 100; 5 6int& getValue() 7{ 8 return value; 9} 10 11int main() 12{ 13 getValue() = 200; 14 15 cout << value; 16 17 return 0; 18}
Output
1200
Note: Never return a reference to a local variable because it is destroyed when the function ends, leaving a dangling reference.
4. const Reference
A const reference allows you to access a variable without modifying it.
It is commonly used when passing large objects to functions efficiently while preventing accidental changes.
Example
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6void display(const string& message) 7{ 8 cout << message; 9} 10 11int main() 12{ 13 string text = "Welcome to C++"; 14 15 display(text); 16 17 return 0; 18}
Output
1Welcome to C++
Reference vs Pointer
| Reference | Pointer |
|---|---|
| Must be initialized | Can be uninitialized |
| Cannot be changed after initialization | Can point to different variables |
Cannot be nullptr | Can be nullptr |
| Accessed directly | Dereferenced using * |
| Simpler syntax | More flexible |
Best Practices
- Initialize references when declaring them.
- Use pass-by-reference for large objects to improve performance.
- Use
constreferences when the object should not be modified. - Never return references to local variables.
- Prefer references over pointers when ownership or nullability is not required.
Practice Problems
- Create a reference variable and modify the original value.
- Swap two numbers using pass-by-reference.
- Write a function that returns a reference to a global variable.
- Pass a string to a function using a
constreference. - Compare pass-by-value and pass-by-reference using a simple program.
Interview Questions
- What is a reference in C++?
- How is a reference different from a pointer?
- What is pass-by-reference?
- Why are references more efficient than pass-by-value for large objects?
- What is a
constreference? - Can a reference be reassigned to another variable?
- Why should you avoid returning references to local variables?
Summary
In this module, you learned:
- What reference variables are and how they act as aliases.
- How pass-by-reference allows functions to modify original variables.
- How functions can return references safely.
- Why
constreferences improve performance and prevent accidental modification. - The key differences between references and pointers.
Next Module: Module 10: Object-Oriented Programming (OOP) — Classes, Objects, Constructors, Destructors, Encapsulation, Inheritance, Polymorphism, Abstraction, Virtual Functions, Friend Functions, and Operator Overloading.