what is the difference between assignment in the instance of the class as following:
- name=n
- this->name =n
what is the difference between assignment in the instance of the class as following:
@gauravlodhi21 there is no difference, this->name just means that we are referring to the data member of the current object. It looks more like the normal way of referring to data members like obj->name but just saying name also works because there is a name variable in the local scope.
If there is a function like
void function(int num) {
this->num = num;
}
in this case this->num refers to the num data member, but num refers to the argument. So this is one way is which you can practically use this keyword. Otherwise if no variables share the same name, there is no difference between the two.