+ 9
Why do we use (this->) when assigning values to properties in c++ classes using constructors
2 Answers
+ 7
We are accessing that objectâs members and âthisâ is a keyword that points to the current object. It is not necessary unless you have parameters with the same name as the member you are trying to access. For example:
class myClass {
public:
myClass(int hi) {
//fine
hello = hi;
//not fine
hi = hi;
//instead
this->hi = hi;
}
int hello;
int hi;
};
If you donât include this-> when there is a parameter with the same name, it will assume that you are talking about the parameter, not the member.
+ 5
thanks man @Jacob Pembleton this is the answer i was looking forâïž