🔵 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.

Frequently Asked Questions

What is a reference in C++?
A reference is an alias — another name for an existing variable. Once 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++?
A reference is an alias that must be initialised, cannot be null, and cannot be reseated to another variable, and you use it just like the original. A pointer holds an address, can be null, can be reassigned, and needs the * operator to reach its value. References are usually safer and cleaner.
Why use references as function parameters?
Passing by reference lets a function modify the caller's variable and avoids copying large objects, which is faster. Writing 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++?
A const reference, written 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++?
No. A reference must be bound to a valid variable when it is created and cannot be null, which is one reason references are considered safer than pointers. If you need something that can point to nothing, use a pointer with nullptr instead.

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 से साफ़ हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.