C++ this Keyword
The this keyword in C++ is a pointer that refers to the current instance of a class. It is implicitly passed to all non-static member functions and provides a way to access the calling object’s members from within the class.
The this pointer is particularly useful for differentiating between class members and function parameters with the same name, method chaining, and returning the current object.
Syntax
</>
Copy
class ClassName {
void methodName() {
this->memberName; // Access the current object's member
}
};
- this
- A pointer to the current object, allowing access to its members.
Examples
Example 1: Differentiating Between Class Members and Parameters
In this example, you will learn how this is used to resolve name conflicts between class members and function parameters.
</>
Copy
#include <iostream>
using namespace std;
class Rectangle {
int length;
int width;
public:
Rectangle(int length, int width) {
this->length = length; // Resolves name conflict
this->width = width; // Resolves name conflict
}
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};
int main() {
Rectangle rect(10, 5);
rect.display();
return 0;
}
Output:
Length: 10, Width: 5
Explanation:
- Inside the constructor, the parameters
lengthandwidthshadow the class members. - The
thispointer is used to explicitly refer to the class members. - This resolves the name conflict and ensures the correct assignment of values.
Example 2: Method Chaining Using this
The this pointer allows method chaining by returning the current object from a member function.
</>
Copy
#include <iostream>
using namespace std;
class Calculator {
int value;
public:
Calculator() : value(0) {}
Calculator& add(int x) {
value += x;
return *this; // Return current object
}
Calculator& subtract(int x) {
value -= x;
return *this; // Return current object
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Calculator calc;
calc.add(10).subtract(3).display();
return 0;
}
Output:
Value: 7
Explanation:
- Each method modifies the
valuemember and returns the current object using*this. - This allows multiple method calls to be chained together in a single statement.
- The final call to
display()prints the modified value.
Example 3: Passing the Current Object
The this pointer can be used to pass the current object as an argument to another function or method.
</>
Copy
#include <iostream>
using namespace std;
class Person {
string name;
public:
Person(string name) : name(name) {}
void greet(const Person* other) const {
cout << "Hello, " << other->name << "! I'm " << name << "." << endl;
}
void introduceYourself() const {
greet(this); // Pass current object
}
};
int main() {
Person p("Alice");
p.introduceYourself();
return 0;
}
Output:
Hello, Alice! I'm Alice.
Explanation:
- The
introduceYourselfmethod calls thegreetmethod, passing the current object as an argument usingthis. - The
greetmethod accesses the current object’s data through the pointer. - This demonstrates how
thiscan be used to share object context with other methods or functions.
Key Points to Remember about this Keyword
- The
thispointer is implicitly available in all non-static member functions. - It refers to the current object and is useful for accessing members, resolving name conflicts, and method chaining.
- The
thispointer cannot be modified. - It is not available in static member functions because static members are not tied to any particular object.
