In most cases, unnecessary disclosure of the this pointer to access the non-static data member of the class instance is optional, but it can help with naming confusion, especially when the members of the class data are defined in a separate header file from the code module. However, you must use the this pointer if you are accessing a non-static data member, which is a member of the base class, which is a template. In other words, in a situation such as:
template<typename T> class base_class { protected: int a; }; template<typename T> class derived_class : public base_class<T> { void function() { a = 5;
you'll notice that you must use the this pointer to properly resolve the inherited non-static data member from the template base class. This is due to the fact that base_class<T>::a is a dependent name, in this case depending on the template parameter T , but when used without the this pointer, it is considered as an independent name and, therefore, cannot be viewed in the dependent namespace of the base class . Thus, without specifically dereferencing the this pointer, you will get a compiler error, for example, β a not been declared in this areaβ or something like that.
Jason source share