Reference Variable and this Pointer in C++
Last Updated on: 18th Dec 2025 17:42:35 PM
1. Introduction to Reference Variable
A reference variable in C++ is an alternative name (alias) for an already existing variable.
Once a reference is created, it cannot refer to another variable.
π A reference variable does not create new memory; it shares the same memory location.
2. Real-Life Example of Reference Variable
Example: Nickname of a Person
-
A person may have a real name and a nickname
-
Both names refer to the same person
π Changing the nickname still refers to the same person
π Reference variable works the same way
3. Syntax of Reference Variable
data_type &reference_name = existing_variable;
4. Example: Basic Reference Variable
A reference acts as another name for the same variable. Any change made using reference affects the original variable.
Complete Program
#include <iostream>
using namespace std;
int main() {
int a = 10;
int &ref = a;
ref = 20;
cout << "Value of a: " << a << endl;
cout << "Value of ref: " << ref << endl;
return 0;
}
Output
Value of a: 20
Value of ref: 20
Explanation of Output
-
ref refers to a
-
Updating ref also updates a
-
Both share the same memory
5. Memory Representation of Reference Variable
a ----> 20
ref ----^
6. Reference Variable vs Pointer
| Reference | Pointer |
|---|---|
| Cannot be NULL | Can be NULL |
| Cannot change reference | Can change address |
No * operator needed |
Needs dereferencing |
| Safer | Risky if not handled properly |
7. Reference Variable in Function (Call by Reference)
Reference variables allow actual parameters to be modified inside a function.
Program
#include <iostream>
using namespace std;
void update(int &x) {
x = x + 5;
}
int main() {
int num = 10;
update(num);
cout << "Updated value: " << num << endl;
return 0;
}
Output
Updated value: 15
Explanation of Output
-
Function receives reference of
num -
Changes affect the original variable
Advantages of Reference Variables
-
No extra memory usage
-
Faster than pointers
-
Cleaner syntax
-
Safer than pointers
PART 2: this Pointer in C++
9. Introduction to this Pointer
The this pointer is an implicit pointer available inside all non-static member functions of a class.
π It holds the address of the current calling object.
10. Real-Life Example of this Pointer
Example: Teacher Calling Herself
If a teacher says:
“I will check the homework”
Here, “I” refers to the current teacher.
π In C++, this works as “I” for objects.
11. Why this Pointer is Needed?
-
To differentiate data members and parameters with same name
-
To return the calling object
-
To access current object’s members
12. Basic Example of this Pointer
When local variables have the same name as class variables, this helps resolve ambiguity.
Complete Program
#include <iostream>
using namespace std;
class Student {
int id;
public:
void setId(int id) {
this->id = id;
}
void show() {
cout << "Student ID: " << id << endl;
}
};
int main() {
Student s;
s.setId(101);
s.show();
return 0;
}
Output
Student ID: 101
Explanation of Output
-
this->id refers to class variable
-
id refers to function parameter
13. Using this Pointer to Return Current Object
this pointer can return the current object, enabling method chaining.
Example
#include <iostream>
using namespace std;
class Box {
int length;
public:
Box(int length) {
this->length = length;
}
Box& increase() {
length += 5;
return *this;
}
void show() {
cout << "Length: " << length << endl;
}
};
int main() {
Box b(10);
b.increase().increase().show();
return 0;
}
Output
Length: 20
Explanation of Output
-
increase() returns current object
-
Method chaining increases value twice
14. this Pointer vs Reference Variable
| this Pointer | Reference Variable |
|---|---|
| Implicit | Explicit |
| Points to object | Alias to variable |
| Used in class | Used anywhere |
| Holds address | Shares memory |
15. Interview Questions
-
What is a reference variable?
-
Can a reference be NULL?
-
What is this pointer?
-
Why this cannot be used in static functions?
-
Difference between pointer and reference?
16. Summary
-
Reference variable is an alias to an existing variable
-
Used for efficient parameter passing
-
this pointer represents current object
-
Helps in resolving ambiguity and method chaining
-
Both are key concepts in OOP and modern C++
Keep practicing — you're doing amazing!
Happy Coding! ![]()