References in C++
What is a reference?
A reference is simply another name — an alias — for a variable that already exists. Whatever you do to the reference happens to the original, because they are the same thing under two names.
Creating a reference
Use & in the declaration and bind it to an existing variable straight away:
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &r = x; // r is an alias for x
r = 20; // changes x
cout << x << " " << r << "\n";
return 0;
}20 20
A reference must be initialised when declared, and it can never be rebound to a different variable afterwards.
References as parameters
The most common use: let a function change the caller's variable, without pointers:
void swap(int &a, int &b) {
int t = a; a = b; b = t;
}
int main() {
int x = 1, y = 2;
swap(x, y);
cout << x << " " << y; // 2 1
}2 1
const references
A const reference lets a function read a big value efficiently (no copy) while promising not to change it:
void print(const string &s) { // no copy, cannot modify s
cout << s << "\n";
}This is the standard, efficient way to pass strings, vectors and other large objects you only need to read.
Returning a reference
Functions can return a reference, but never return a reference to a local variable — it is destroyed when the function ends. Returning a reference to something that outlives the call (like an array element) is fine.
References vs pointers
| Reference | Pointer | |
|---|---|---|
| Can be null? | No | Yes (nullptr) |
| Must be initialised? | Yes | No |
| Can be reseated? | No | Yes |
| Access value | Use like the variable | Needs * |
See the pointers lesson for when a pointer is the right choice.
Common mistakes
- Forgetting to initialise a reference — it must bind at declaration.
- Expecting to reseat a reference to a new variable later (not possible).
- Returning a reference to a local variable (dangling).
- Copying a big object by value when a
constreference would be faster.
Write a function that takes an int& and doubles it. Then write a print(const vector<int>&) that prints a list without copying it. Notice how the caller's data is affected in each case.
Summary
- A reference is an alias for an existing variable.
- It must be initialised and can never be reseated or null.
- Reference parameters let functions change the caller's data.
constreferences pass large values efficiently and safely.- References are usually cleaner than pointers for everyday use.
Frequently Asked Questions
What is a reference in C++?
int &r = x; binds r to x, using r is exactly like using x. References must be initialised when declared and cannot later refer to a different variable.What is the difference between a reference and a pointer in C++?
* operator to reach its value. References are usually safer and cleaner.Why use references as function parameters?
void f(int &x) means changes to x inside f are seen by the caller, unlike pass-by-value which copies.What is a const reference in C++?
const T&, lets a function read a large object without copying it while promising not to change it. It is the standard way to pass big values like strings or vectors efficiently and safely.Can a reference be null in C++?
nullptr instead.Reference क्या है?
Reference बस किसी पहले से मौजूद variable का दूसरा नाम — एक उपनाम — है। आप reference के साथ जो भी करें वह मूल के साथ होता है, क्योंकि वे दो नामों के तहत एक ही चीज़ हैं।
Reference बनाना
Declaration में & इस्तेमाल करें और इसे तुरंत किसी मौजूदा variable से बाँधें:
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &r = x; // r, x ka alias hai
r = 20; // x badalta hai
cout << x << " " << r << "\n";
return 0;
}20 20
Reference को declare करते समय initialise करना ज़रूरी है, और यह बाद में कभी किसी अलग variable से फिर से बँध नहीं सकता।
Parameters के रूप में references
सबसे आम इस्तेमाल: किसी function को बिना pointers के caller का variable बदलने देना:
void swap(int &a, int &b) {
int t = a; a = b; b = t;
}
int main() {
int x = 1, y = 2;
swap(x, y);
cout << x << " " << y; // 2 1
}2 1
const references
const reference किसी function को बड़ी value कुशलता से (बिना copy) पढ़ने देता है और साथ ही न बदलने का वादा करता है:
void print(const string &s) { // no copy, s badal nahi sakta
cout << s << "\n";
}यह strings, vectors और अन्य बड़े objects भेजने का मानक, कुशल तरीका है जिन्हें आपको केवल पढ़ना है।
Reference लौटाना
Functions reference लौटा सकते हैं, पर कभी local variable का reference न लौटाएँ — function समाप्त होने पर वह नष्ट हो जाता है। ऐसी चीज़ का reference लौटाना जो call से आगे जीवित रहे (जैसे array element) ठीक है।
References बनाम pointers
| Reference | Pointer | |
|---|---|---|
| Null हो सकता है? | नहीं | हाँ (nullptr) |
| Initialise ज़रूरी? | हाँ | नहीं |
| Reseat हो सकता है? | नहीं | हाँ |
| Value access | variable की तरह इस्तेमाल | * चाहिए |
Pointer कब सही विकल्प है इसके लिए pointers lesson देखें।
आम गलतियाँ
- Reference initialise करना भूलना — इसे declaration पर बँधना होगा।
- बाद में reference को नए variable पर reseat करने की उम्मीद करना (संभव नहीं)।
- Local variable का reference लौटाना (dangling)।
- बड़ा object value से copy करना जब
constreference तेज़ होता।
एक function लिखें जो int& ले और उसे दोगुना करे। फिर एक print(const vector<int>&) लिखें जो सूची को बिना copy किए print करे। देखें हर मामले में caller का data कैसे प्रभावित होता है।
सारांश
- Reference किसी मौजूदा variable का उपनाम है।
- इसे initialise करना ज़रूरी है और यह कभी reseat या null नहीं हो सकता।
- Reference parameters functions को caller का data बदलने देते हैं।
constreferences बड़ी values कुशलता और सुरक्षा से भेजते हैं।- रोज़मर्रा के इस्तेमाल के लिए references आमतौर पर pointers से साफ़ हैं।