this Pointer in C++ - Definition, Uses, Syntax and Examples

Introduction to this Pointer in C++

In C++, the this pointer is a special pointer available inside non-static member functions of a class. It points to the current object that invokes the member function. The this pointer is used to access the data members and member functions of the current object. It is particularly useful when local variables and class data members have the same names.

C++ में this pointer एक विशेष pointer है, जो class के non-static member functions के अंदर उपलब्ध होता है। यह उस current object को point करता है जिसने member function को call किया है। this pointer का उपयोग current object के data members और member functions को access करने के लिए किया जाता है। यह विशेष रूप से तब उपयोगी होता है जब local variables और class data members के नाम समान हों।

Syntax of this Pointer

The this pointer can be used with the arrow operator (->) to access the members of the current object.

Current object के members को access करने के लिए this pointer के साथ arrow operator (->) का उपयोग किया जाता है।

this->dataMember;
this->memberFunction();

Here, this->dataMember refers to the data member of the current object.

यहाँ this->dataMember current object के data member को refer करता है।

Example 1: Simple Program Using this Pointer

The following program demonstrates how the this pointer accesses the data member of the current object.

निम्न program दिखाता है कि this pointer current object के data member को कैसे access करता है।

#include <iostream>
using namespace std;
class Student {
public:
int rollNo;
void setRollNo(int rollNo) {
this->rollNo = rollNo;
}
void display() {
cout << "Roll Number = " << this->rollNo;
}
};
int main() {
Student s1;
s1.setRollNo(101);
s1.display();
return 0;
}

Output:

Roll Number = 101

Explanation:

  • The class Student has a data member named rollNo.

  • The function setRollNo() also has a parameter named rollNo.

  • this->rollNo refers to the data member of the current object.

  • The plain rollNo refers to the function parameter.

  • Therefore, this->rollNo = rollNo; assigns the parameter value to the object's data member.

व्याख्या:

  • Student class में rollNo नाम का data member है।

  • setRollNo() function में भी rollNo नाम का parameter है।

  • this->rollNo current object के data member को refer करता है।

  • केवल rollNo लिखने पर function parameter refer होता है।

  • इसलिए this->rollNo = rollNo; statement parameter की value को object के data member में assign करता है।

Example 2: Using this Pointer to Call a Member Function

The this pointer can also be used to call another member function of the current object.

this pointer का उपयोग current object के किसी दूसरे member function को call करने के लिए भी किया जा सकता है।

#include <iostream>
using namespace std;
class Student {
public:
int rollNo;
void setData(int r) {
this->rollNo = r;
}
void display() {
cout << "Roll Number = " << this->rollNo << endl;
}
void show() {
this->display();
}
};
int main() {
Student s1;
s1.setData(105);
s1.show();
return 0;
}

Output:

Roll Number = 105

Explanation:

The show() member function calls the display() function using this->display(). It invokes the member function for the current object.

show() member function में this->display() का उपयोग करके display() function को call किया गया है। इससे current object का member function execute होता है।

Example 3: Using this Pointer to Return the Current Object

The this pointer can be dereferenced using *this to refer to the current object. It can be returned by reference to support method chaining.

this pointer को *this के माध्यम से dereference करके current object को refer किया जा सकता है। इसे reference के रूप में return करके method chaining की सुविधा प्राप्त की जा सकती है।

Program:

#include <iostream>
using namespace std;
class Number {
public:
int value;
Number(int v = 0) {
value = v;
}
Number& setValue(int v) {
this->value = v;
return *this;
}
void display() {
cout << "Value = " << value << endl;
}
};
int main() {
Number n;
n.setValue(10).display();
return 0;
}

Output:

Value = 10

Explanation:

  • setValue() changes the value of the current object.

  • return *this; returns the current object by reference.

  • Since the function returns a reference, another member function can be called on the same object.

  • This technique is known as Method Chaining.

व्याख्या:

  • setValue() current object की value को change करता है।

  • return *this; current object को reference के रूप में return करता है।

  • क्योंकि function reference return करता है, इसलिए उसी object पर अगला member function call किया जा सकता है।

  • इस technique को Method Chaining कहते हैं।

Characteristics of this Pointer in C++

  1. The this pointer is available inside non-static member functions.
    this pointer non-static member functions के अंदर उपलब्ध होता है।

  2. It points to the current object that invokes the member function.
    यह उस current object को point करता है जो member function को call करता है।

  3. It is used to access the data members and member functions of the current object.
    इसका उपयोग current object के data members और member functions को access करने के लिए किया जाता है।

  4. It helps distinguish between class data members and local variables or function parameters with the same name.
    यह समान नाम वाले class data members और local variables या function parameters में अंतर करने में मदद करता है।

  5. The this pointer cannot be used directly inside static member functions because static member functions are not associated with a particular object.
    Static member functions में this pointer का सीधे उपयोग नहीं किया जा सकता क्योंकि static member functions किसी विशेष object से संबंधित नहीं होते।

  6. In a const-qualified member function, this behaves as a pointer to a const object.
    Const-qualified member function में this, const object के pointer की तरह व्यवहार करता है।

Difference Between this Pointer and Normal Pointer

this PointerNormal Pointer
It is a special pointer available in non-static member functions.It is an ordinary pointer variable declared by the programmer.
It points to the current object.It can point to different variables, objects or memory locations.
It is automatically available in non-static member functions.It must be declared and initialized or assigned.
It cannot be reassigned to point to another object.It can be reassigned to point to another valid object or memory location.

Advantages of this Pointer

  • Avoids Name Conflicts: It distinguishes data members from function parameters with identical names.
    Name Conflicts से बचाव: यह समान नाम वाले data members और function parameters में अंतर करता है।

  • Accesses Current Object: It provides access to the members of the object that calls the function.
    Current Object का Access: यह function को call करने वाले object के members को access करने की सुविधा देता है।

  • Supports Method Chaining: It can return the current object by reference, allowing multiple function calls in a single statement.
    Method Chaining: यह current object को reference के रूप में return करके एक ही statement में multiple function calls की सुविधा देता है।

The this pointer is an important feature of C++ that refers to the current object inside non-static member functions. It helps access current object members, resolve naming conflicts and implement method chaining.

C++ में this pointer एक महत्वपूर्ण सुविधा है, जो non-static member functions के अंदर current object को refer करता है। इसका उपयोग object के members को access करने, समान नाम वाले variables में अंतर करने और method chaining implement करने के लिए किया जाता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)