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.
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 से साफ़ हैं।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में reference क्या है?
int &r = x; r को x से बाँध दे, r इस्तेमाल करना बिल्कुल x इस्तेमाल करने जैसा है। References को declare करते समय initialise करना ज़रूरी है और बाद में किसी अलग variable को refer नहीं कर सकते।C++ में reference और pointer में क्या अंतर है?
* operator चाहिए। References आमतौर पर ज़्यादा सुरक्षित और साफ़ हैं।Function parameters के रूप में references क्यों इस्तेमाल करें?
void f(int &x) लिखने का मतलब f के अंदर x में बदलाव caller को दिखते हैं, pass-by-value के विपरीत जो copy करता है।C++ में const reference क्या है?
const T& लिखा जाता है, किसी function को बड़े object को बिना copy किए पढ़ने देता है और साथ ही न बदलने का वादा करता है। यह strings या vectors जैसी बड़ी values को कुशलता और सुरक्षा से भेजने का मानक तरीका है।क्या C++ में reference null हो सकता है?
nullptr वाला pointer इस्तेमाल करें।