When should I explicitly write this->member in a method of
a class?
When should I explicitly write this->member in a method of a class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Usually, you do not have to,
this->is implied.Sometimes, there is a name ambiguity, where it can be used to disambiguate class members and local variables. However, here is a completely different case where
this->is explicitly required.Consider the following code:
If you omit
this->, some compilers do not know how to treati. In order to tell it thatiis indeed a member ofA<T>, for anyT, thethis->prefix is required.Note: it is possible to still omit
this->prefix by using: