THIS FUNCTION IN C++
INTRODUCTION:
In C++ programming, this can be a catchphrase that alludes to the current occurrence of the course. There can be 3 primary utilization of this watchword in C++ . It can be utilized to pass current protest as a parameter to another method. It can be utilized to allude current course occasion variable. It can be utilized to pronounce indexers.
In C++, there are 4 methods to utilise this keyword in a class:
Fix Shadowing Problem With This Keyword.
Use this Keyword to Access the Currently Executing Object
Members Can Access Data Using This Keyword.
Member Functions Can Be Called Using This Keyword.
Resolve Shadowing Issue Using this Keyword:
When a nearby variable and a part variable of a course have the same title, shadowing issues can be fathomed in C by utilizing the this pointer. The this pointer permits you to expressly get to the part variable and alludes to the current occasion of the class.
PROGRAMME:
#include <iostream>
using namespace std;
class Example {
private:
int value;
public:
Example(int value) {
this->value = value;
}
void setValue(int value) {
// Use "this->value" to refer to the member variable
this->value = value;
}
int getValue() {
return value;
}
};
int main() {
Example obj(10);
cout << "Initial value: " << obj.getValue() << endl;
int value = 20;
obj.setValue(value);
cout << "Updated value: " << obj.getValue() << endl;
return 0;
}
Output:
![THIS FUNCTION IN C++](https://static.tutorialandexample.com/cpp/this-function-in-cpp1.png)
EXPLANATION:
- The initial value of a new Example object called obj is set to be 10.
- Obj. getValue() prints "Initial value: 10" as the initial value of the object.
- The value 20 is defined as a local variable with the name value.
- The local value is sent as a parameter when the set Value method of the obj object is invoked. This changes the obj object's member variable value to 20.
- obj. getValue() prints "Updated value: 20" as the updated value of the object.
- The programme returns 0, indicating that it has run successfully.
- Remember that the code shows how to utilise the this pointer to prevent shadowing and to explicitly access the class member value.
Access Currently Executing Object Using this Keyword:
Through its member functions, this term may be used to chain functions and remove objects.
The current object, or the object being used to invoke the member function, is referred to within a member function in C++ by the this pointer. Even if there are local variables or parameters with the identical names, it enables you to immediately access the members and methods of the object.
Here is an illustration showing how to access the object that is presently running in C++ using the this keyword:
PROGRAMME:
#include <iostream>
class MyClass {
private:
int value;
public:
void setValue(int val) {
this->value = val;
}
int getValue() {
return this->value;
}
};
int main() {
MyClass obj;
obj.setValue(42);
std::cout << "Value: " << obj.getValue() << std::endl;
return 0;
}
Output:
![THIS FUNCTION IN C++](https://static.tutorialandexample.com/cpp/this-function-in-cpp2.png)
EXPLANATION:
In the above example, the 'this' pointer is utilised within the member procedures setValue() and getValue() to access the current object's member variable value. It is not always required to say 'this,' especially if there is no doubt about which variable or method is being discussed. However, in circumstances when the scope can generate ambiguity or when you want to make it clear that you're referring to a member of the current instance, using 'this' might be useful.Remember that 'this' is always available inside the scope of a class's non-static member functions. It's simply a secret parameter that is given behind the scenes to the member functions.
Let's look at some of the benefits:
Clarity and Disambiguation: In a class, member variables having the same names as parameters in member functions are permitted. Using the "this" pointer explicitly indicates which variable is being referred to. This removes any possibility of mistake between the local parameter and the class member.
Using the "this" pointer, you may access class member variables and methods from within a member function, even if their names are the same as local variables. This is critical for distinguishing between local variables and class members.
Member Variable Assignment: When you have both local variables and member variables with the same name, the "this" pointer assists you in distinguishing between them. It allows you to assign values to specified member variables.
Chaining Methods: The "this" reference is required for chaining method calls on the same object. This is frequently used in C++ libraries and APIs to provide more expressive and understandable code.
Non-static member functions are implicitly supplied the "this" reference as a hidden parameter. This allows functions to determine which object they are functioning on. It can be handy for putting certain design patterns into action.
Example:
#include <iostream>
class Rectangle {
private:
int width;
int height;
public:
Rectangle(int w, int h) : width(w), height(h) {}
void setDimensions(int width, int height) {
this->width = width; // Using 'this' to set member variables
this->height = height; // without ambiguity
}
int area() {
return this->width * this->height; // Accessing members with 'this'
}
};
int main() {
Rectangle rect(5, 10);
rect.setDimensions(3, 6);
std::cout << "Area: " << rect.area() << std::endl;
return 0;
}
Output:
![THIS FUNCTION IN C++](https://static.tutorialandexample.com/cpp/this-function-in-cpp3.png)
EXPLANATION:
The code you gave declares a Rectangle class, then creates an instance of that class, adjusts its dimensions using the setDimensions() function, and calculates and outputs the area with the area() method.