In this article, we will understand more about the this pointer as well as understand more about it’s use case.
The This Pointer
This is the pointer whose type is the same as the class type. Okay, but where does it points?
Yeah, it points to the current instance of the class(object). Let’s prove this by an example.
#include <iostream> #include <stdlib.h> using namespace std; class ThisExample { public: ThisExample() { cout << "In constructor" << endl; } void printThis() { cout << "This:" << this << endl; } }; int main(void) { ThisExample* a = new ThisExample(); cout <<" Pointer A: " << a << endl; a->printThis(); delete a; // cleaning the memory }
The address of this and the class object is same. It means that every object has an access to it’s own address through this. You can access it’s member function and variable using this.
Use of this keyword
You can use this when the local variable is same as the class variable and you want to assign the value to the class variable.
#include <iostream> #include <stdlib.h> using namespace std; class ThisEx { public: ThisEx() { b = 0; // It is good practise to initialise class variables in constructor c = 0; } void saveVars(int b, int c) { this->b = b; this->c = c; } void showVars() { cout<<"B = "<< b << " C = "<< c << endl; } private: int b; int c; }; int main(void) { int a = 10, b = 20; ThisEx* thisEx = new ThisEx(); thisEx->showVars(); thisEx->saveVars(a , b); thisEx->showVars(); delete thisEx; // cleaning the memory }
You should also use this when you have to pass the reference of the present class to the another class. Consider this example.
Header File
class ThisEx1 { public: class ThisEx2; ThisEx1() { b = 0; // It is good practise to initialise class variables in constructor c = 0; } void saveVars(int b, int c); void showVars(); void callThis2(); private: int b; int c; }; class ThisEx1::ThisEx2 { public: ThisEx2(){ } void setClass(ThisEx1* thisEx1); void executecallback(); private: ThisEx1* _thisEx1; };
Main Code
#include <iostream> #include <stdlib.h> #include "ThisClass.h" using namespace std; void ThisEx1::saveVars(int b, int c) { this->b = b; this->c = c; } void ThisEx1::showVars() { cout << "B = "<< b << " C = "<< c <<endl; } void ThisEx1::callThis2() { ThisEx1::ThisEx2 thisex2; thisex2.setClass(this); thisex2.executecallback(); } void ThisEx1::ThisEx2::setClass(ThisEx1* thisEx1) { _thisEx1 = thisEx1; } void ThisEx1::ThisEx2::executecallback() { _thisEx1->saveVars(30,40); _thisEx1->showVars(); } int main(void) { ThisEx1* thisEx = new ThisEx1(); thisEx->callThis2(); delete thisEx; // cleaning the memory }
In the above example, what I am really doing is saving the instance of one class into another class and calling the function of the first class from the second class. This type of approach is used in callbacks. Also, don’t be overwhelmed by his example. Think “ThisEx1::ThisEx2” as another class.
This can also be used as a function chaining. See this example
#include<iostream> using namespace std; class A { private: int x; int y; public: A(int x = 0, int y = 0) { this->x = x; this->y = y; } A* setX(int x) { this->x = x; return this; } A* setY(int y) { this->y = y; return this; } void print() { cout << "x = " << x << " y = " << y << endl; } }; int main() { A* obj1 = new A(6,4); obj1->setX(10)->setY(20); obj1->print(); delete obj1; return 0; }
In the above example, the SetX and SetY function returns this and that’s how you are able to chain the function calls.
These are the use cases where you should use this. I think that’s it for this article. If you found anything wrong or have questions or feedback, do ask me in the comment section.