I have seen recommended that member functions refer to member variables using this-> explicitly, to avoid forgetting to declare the member variable and accidentally referring to a global variable. Can anyone comment if they think this is a reasonable precaution or unnecessary verbosity that will needlessly complicate the reading of the code? Does anybody actually do this in day-to-day real-life programming?
I have seen recommended that member functions refer to member variables using this-> explicitly,
Share
Since I’m frequently implementing class templates, I’m used to always qualify member variables and member functions with
this->. In the context of class templates there is another reason why it is useful to qualify names withthis->: Names not depending on template arguments are looked up only in phase I look-up, i.e., names in base classes depending on template arguments are never looked up in the base. By qualifying member names withthis->they become dependent. For example:I’m using qualification with
this->in all my code unless some coding guidelines prohibits me from doing so (and I’d consider this aspect of the coding guideline silly and wrong). Of course, in my own code I qualify all names which can be qualified and which aren’t a customization point (e.g., I wouldn’t qualifyswap()).