+ 9

Why do we use (this->) when assigning values to properties in c++ classes using constructors

22nd Jan 2018, 3:56 PM
Sanoj Silva
Sanoj Silva - avatar
2 Réponses
+ 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.
22nd Jan 2018, 5:14 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 5
thanks man @Jacob Pembleton this is the answer i was looking for✌️
22nd Jan 2018, 10:18 PM
Sanoj Silva
Sanoj Silva - avatar