I have implemented the above code without using the 'this' pointer and its working fine... is this okay to de or do we have to use this pointer?
Hello @Ak07,
It seems like if you have not understood the concept of ‘this’ pointer.
this is used to prevent the name conflict inside the class.
When do you need “this”?
Suppose, the name of the parameter passed accepted by the member function and the class data member is the same.
Then, this is used to distinguish the data member from the parameter.
Example:
class node{
public:
int data;
node * next;
node(int data){
data = data; // this will cause warning/error
//Thus to avoid this we use this pointer as
this->data=data;
}
};
Hope, this would help.
Give a like if you are satified.
Thanks, i got this… So in this class the parameter passed and the class data member are not the same so its okay to not use this pointer right ( i am asking this cus in the video also there was not a single same parameter as class data member and they still used this pointer.)
Hello @Ak07,
Yes, you have understood it know.
He used it because he has a habit of writing and moreover, it increases the readability.
A new person would easily be able to read the code.
As, you might sometimes use many variable (data members and those local to member function).
So, it visually makes it easier to pick the data members from normal.
And it’s an option and thus your choice whether to use it or not in above case.