🔵 Core C++  ·  Lesson 25

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:

C++
#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;
}
Output:
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:

C++
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
}
Output:
2 1

const references

A const reference lets a function read a big value efficiently (no copy) while promising not to change it:

C++
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

ReferencePointer
Can be null?NoYes (nullptr)
Must be initialised?YesNo
Can be reseated?NoYes
Access valueUse like the variableNeeds *

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 const reference would be faster.
🏋️ Practice

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.
  • const references pass large values efficiently and safely.
  • References are usually cleaner than pointers for everyday use.

Reference क्या है?

Reference बस किसी पहले से मौजूद variable का दूसरा नाम — एक उपनाम — है। आप reference के साथ जो भी करें वह मूल के साथ होता है, क्योंकि वे दो नामों के तहत एक ही चीज़ हैं।

Reference बनाना

Declaration में & इस्तेमाल करें और इसे तुरंत किसी मौजूदा variable से बाँधें:

C++
#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;
}
Output:
20 20

Reference को declare करते समय initialise करना ज़रूरी है, और यह बाद में कभी किसी अलग variable से फिर से बँध नहीं सकता।

Parameters के रूप में references

सबसे आम इस्तेमाल: किसी function को बिना pointers के caller का variable बदलने देना:

C++
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
}
Output:
2 1

const references

const reference किसी function को बड़ी value कुशलता से (बिना copy) पढ़ने देता है और साथ ही न बदलने का वादा करता है:

C++
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

ReferencePointer
Null हो सकता है?नहींहाँ (nullptr)
Initialise ज़रूरी?हाँनहीं
Reseat हो सकता है?नहींहाँ
Value accessvariable की तरह इस्तेमाल* चाहिए

Pointer कब सही विकल्प है इसके लिए pointers lesson देखें।

आम गलतियाँ

  • Reference initialise करना भूलना — इसे declaration पर बँधना होगा।
  • बाद में reference को नए variable पर reseat करने की उम्मीद करना (संभव नहीं)।
  • Local variable का reference लौटाना (dangling)।
  • बड़ा object value से copy करना जब const reference तेज़ होता।
🏋️ अभ्यास

एक function लिखें जो int& ले और उसे दोगुना करे। फिर एक print(const vector<int>&) लिखें जो सूची को बिना copy किए print करे। देखें हर मामले में caller का data कैसे प्रभावित होता है।

सारांश

  • Reference किसी मौजूदा variable का उपनाम है।
  • इसे initialise करना ज़रूरी है और यह कभी reseat या null नहीं हो सकता।
  • Reference parameters functions को caller का data बदलने देते हैं।
  • const references बड़ी values कुशलता और सुरक्षा से भेजते हैं।
  • रोज़मर्रा के इस्तेमाल के लिए references आमतौर पर pointers से साफ़ हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में reference क्या है?
Reference एक उपनाम है — किसी मौजूदा variable का दूसरा नाम। एक बार int &r = x; r को x से बाँध दे, r इस्तेमाल करना बिल्कुल x इस्तेमाल करने जैसा है। References को declare करते समय initialise करना ज़रूरी है और बाद में किसी अलग variable को refer नहीं कर सकते।
C++ में reference और pointer में क्या अंतर है?
Reference एक उपनाम है जिसे initialise करना ज़रूरी है, null नहीं हो सकता, और किसी अन्य variable पर reseat नहीं हो सकता, और आप इसे मूल की तरह ही इस्तेमाल करते हैं। Pointer एक address रखता है, null हो सकता है, फिर से assign हो सकता है, और अपनी value तक पहुँचने को * operator चाहिए। References आमतौर पर ज़्यादा सुरक्षित और साफ़ हैं।
Function parameters के रूप में references क्यों इस्तेमाल करें?
Reference से भेजना किसी function को caller का variable बदलने देता है और बड़े objects की copy से बचाता है, जो तेज़ है। void f(int &x) लिखने का मतलब f के अंदर x में बदलाव caller को दिखते हैं, pass-by-value के विपरीत जो copy करता है।
C++ में const reference क्या है?
const reference, const T& लिखा जाता है, किसी function को बड़े object को बिना copy किए पढ़ने देता है और साथ ही न बदलने का वादा करता है। यह strings या vectors जैसी बड़ी values को कुशलता और सुरक्षा से भेजने का मानक तरीका है।
क्या C++ में reference null हो सकता है?
नहीं। Reference बनाते समय किसी मान्य variable से बँधा होना चाहिए और null नहीं हो सकता, यह एक कारण है कि references pointers से सुरक्षित माने जाते हैं। अगर आपको ऐसा कुछ चाहिए जो किसी चीज़ पर इशारा न करे, इसके बजाय nullptr वाला pointer इस्तेमाल करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.