Chapter 5 Technical Basis: 5.3 Use of this->

5.3 Using this->

5.3 Use of this->

 

For class templates with base classes that depend on template parameters, using a name x by itself is not always equivalent to this->x, even though a member x is inherited. For example:

For class templates with a base class (the base class depends on the template parameter), using the name x itself is often not equivalent to this->. Even if member x is inherited. E.g:

template <typename T>
 class Base {
 public :
     void bar (); 
}; 

template <typename T>
 class Derived: Base <T> {
 public :
     void foo () { 
        bar (); // Call external bar () or Error 
    } 
};

In this example, for resolving the symbol bar inside foo(), bar() defined in Base is never considered. Therefore, either you have an error, or another bar() (such as a global bar()) is called.

In this example, in order to parse the bar symbol in foo (), bar () defined by the base class is never considered. Therefore, either an error occurs here, or another bar () is called (such as the global bar ()).

 

We discuss this issue in Section 13.4.2 on page 237 in detail. For the moment, as a rule of thumb, we recommend that you always qualify any symbol that is declared in a base that is somehow dependent on a template parameter with this-> or Base<T>::.

We will discuss this issue in detail in 13.4.2 on page 237. At present, as a rule of thumb: for symbols that are declared in the base class and depend on template parameters, we always recommend that you should use this-> or base <T> :: to qualify them.

 

Guess you like

Origin www.cnblogs.com/5iedu/p/12731331.html
5.3