Lesson 8 of 19
References
References
A reference is an alias — another name for an existing variable. References are one of C++'s key improvements over C.
Declaring a Reference
int x = 10;
int& ref = x; // ref is an alias for x
ref = 20; // changes x through the alias
cout << x; // 20
A reference must be initialized when declared and cannot be rebound to another variable.
References as Function Parameters
This is the most common use. In C, passing by value creates a copy:
void increment(int x) {
x++; // only changes the local copy
}
With a reference, the function modifies the original:
void increment(int& x) {
x++; // modifies the original
}
int n = 5;
increment(n);
cout << n; // 6
const References
Use const T& to pass large objects without copying and without allowing modification. This is the idiomatic C++ way to pass strings and other large objects to functions:
void greet(const string& name) {
cout << "Hello, " << name << "!" << endl;
// name = "Bob"; // error — const
}
Without const&, passing a string would copy the entire string. With const&, only a pointer-sized alias is passed.
Return by Reference
Functions can also return references to allow chaining or direct modification:
string& getLonger(string& a, string& b) {
return a.length() >= b.length() ? a : b;
}
References vs Pointers
| References | Pointers | |
|---|---|---|
| Syntax | int& r = x | int* p = &x |
| Null | Cannot be null | Can be null |
| Rebind | Cannot rebind | Can reassign |
| Preferred for | Function parameters | Optional/nullable |
Your Task
Write:
printPair(const string& first, const string& second)— prints<first> and <second>combine(const string& a, const string& b, const string& sep)— returnsa + sep + b
Use them in main to produce the expected output.
JSCPP loading...
Loading...
Click "Run" to execute your code.