Why do we use ( this -> ) in parameterized constructor ?
Doubt in Class code
hello @yashsharma4304
this is internal pointer that contains address of ur object . (note : compiler declare this on its own we dont have to delcare or initialise it)
so the stament this->member_variable is just accesing that member variable.
we use this when member variable name is same as local variable to distinguish them ,
there are other use cases as well u can read them from here-> link
so let us say if I write a parameterized constructor as:
Car( string name, int x, int y){
public :
this->name = name;
this->x = x;
this->y = y;
}
Here the string parameter is name therefore we will use (this->) to distinguish both objects. But instead of using name if I write string n then I will write it as:
n = name;
Is this alright ??
yeah correct. . . . . . . . . . . .
Ok thank you for resolving my doubt
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.