BIT153 ยท 2082 ยท 5 marks
What is the use of this pointer? Write a program to demonstrate it.
What is the use of this pointer? Write a program to demonstrate it. [5]
Object Oriented ProgrammingChapter 4: Pointers, References, and Memory ManagementThis pointer concept and uses
Quick answer
The this pointer is an implicit pointer available inside every non-static member function of a class. It automatically points to the object that invoked the member function.
The this Pointer in C++
Definition and Use
Uses of this Pointer
| Use | Description |
|---|---|
| Resolve name conflict | Distinguish between member variables and parameters with the same name |
| Return current object | Return *this to enable method chaining |
| Pass current object | Pass the calling object to another function |
| Self-reference check | Check if two pointers point to the same object |
Program to Demonstrate this Pointer
#include <iostream>
using namespace std;
class Student {
private:
int id;
string name;
float marks;
public:
// Use 1: Resolving name conflict between parameter and data member
void setData(int id, string name, float marks) {
this->id = id; // 'this->id' is member, 'id' is parameter
this->name = name;
this->marks = marks;
}
// Use 2: Returning current object to enable method chaining
Student& setId(int id) {
this->id = id;
return *this; // returns reference to calling object
}
Student& setName(string name) {
this->name = name;
return *this;
}
Student& setMarks(float marks) {
this->marks = marks;
return *this;
}
void display() {
cout << "ID : " << this->id << endl;
cout << "Name : " << this->name << endl;
cout << "Marks : " << this->marks << endl;
}
// Use 3: Compare if two objects are the same using this pointer
bool isSame(Student &s) {
if (this == &s) {
return true; // same object in memory
}
return false;
}
};
int main() {
// Demonstrating Use 1: Resolving name conflict
cout << "--- Using setData (name conflict resolution) ---" << endl;
Student s1;
s1.setData(101, "Ram Sharma", 85.5);
s1.display();
// Demonstrating Use 2: Method chaining using *this
cout << "\n--- Using Method Chaining ---" << endl;
Student s2;
s2.setId(102).setName("Sita Thapa").setMarks(90.0);
s2.display();
// Demonstrating Use 3: Self-reference check
cout << "\n--- Self-reference Check ---" << endl;
if (s1.isSame(s1)) {
cout << "s1 and s1 are the SAME object." << endl;
}
if (!s1.isSame(s2)) {
cout << "s1 and s2 are DIFFERENT objects." << endl;
}
return 0;
}
Output
--- Using setData (name conflict resolution) ---
ID : 101
Name : Ram Sharma
Marks : 85.5
--- Using Method Chaining ---
ID : 102
Name : Sita Thapa
Marks : 90
--- Self-reference Check ---
s1 and s1 are the SAME object.
s1 and s2 are DIFFERENT objects.
Key Points to Remember
thisis a constant pointer (its address cannot be changed).- It holds the address of the calling object:
this == &callingObject - It is not available in static member functions (static functions have no object context).
- Dereferencing it (
*this) gives the actual calling object.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.