Introduction to C++ | this Pointer
C++ this Pointer
The this pointer in C++ is an implicit pointer that points to the object that is calling the member function. It is an essential feature of object-oriented programming (OOP) in C++ and is used primarily within class member functions to refer to the invoking object. The this pointer is automatically passed to all non-static member functions of a class, and it helps in distinguishing between member variables and parameters with the same name.
What is the this Pointer?
The this pointer is a constant pointer (i.e., it cannot be changed to point to another object) that points to the current instance of the class. It allows member functions to access the current object's data members and member functions.
Characteristics of the this Pointer
- It is an implicit pointer, automatically passed to all non-static member functions of a class.
- The this pointer is of type
ClassName*
, whereClassName
is the name of the class. - It cannot be modified or assigned a new value (i.e.,
this
is a constant pointer). - It is used to refer to the current object within a member function.
- It is not available in static member functions because they do not belong to any specific object.
Syntax of this Pointer
The syntax for using the this pointer is straightforward:
Syntax of this Pointer
class ClassName {
public:
void functionName() {
// Accessing data members using 'this' pointer
this->dataMember = value;
}
};
Example of this Pointer
Here’s an example where the this pointer is used to refer to the current object inside a member function:
Code Example: Using this Pointer
#include <iostream>
using namespace std;
class Box {
public:
int length;
// Constructor to initialize length
Box(int length) {
this->length = length; // Using 'this' pointer to distinguish between member and parameter
}
void displayLength() {
cout << "Length: " << this->length << endl; // Using 'this' pointer to access member variable
}
};
int main() {
Box box1(10);
box1.displayLength(); // Output: Length: 10
return 0;
}
Output:
Use of this Pointer in Constructor
The this pointer is often used in constructors when a constructor parameter has the same name as a member variable. It helps to distinguish the member variable from the parameter by explicitly referring to the class member.
Example: this Pointer in Constructor
Here’s an example of how the this pointer is used in the constructor to avoid ambiguity between the constructor parameter and the class data member:
Code Example: this Pointer in Constructor
#include <iostream>
using namespace std;
class Person {
public:
string name;
// Constructor with parameter name
Person(string name) {
this->name = name; // Resolving ambiguity using 'this' pointer
}
void displayName() {
cout << "Name: " << this->name << endl;
}
};
int main() {
Person person1("John");
person1.displayName(); // Output: Name: John
return 0;
}
Output:
Use of this Pointer in Member Function
The this pointer is automatically passed to all non-static member functions, allowing those functions to access the object that called them. The this pointer can be used when referring to member variables or calling other member functions of the same class.
Example: Using this Pointer to Chain Member Functions
In some cases, the this pointer can be used for method chaining. This happens when a member function returns a reference to the current object, allowing you to call another member function on the same object:
Code Example: Method Chaining Using this Pointer
#include <iostream>
using namespace std;
class Box {
public:
int length;
// Constructor to initialize length
Box(int length) {
this->length = length;
}
// Method to set length and return current object for chaining
Box& setLength(int length) {
this->length = length;
return *this; // Return reference to current object
}
void displayLength() {
cout << "Length: " << this->length << endl;
}
};
int main() {
Box box1(10);
box1.setLength(20).displayLength(); // Chaining setLength and displayLength
return 0;
}
Output:
Pro Tip:
💡 Pro Tip
The this pointer can be used to return the current object from a member function, enabling method chaining. Additionally, when the function's parameters and data members have the same name, the this pointer is crucial to avoid ambiguity and ensure the correct value is assigned.