The type lookup process in C + + is relatively simple, basically a name lookup, which is no longer described here. You can learn C + + programming-basic knowledge tutorial.
For a function (or member function) called in a. cpp file, the compiler mainly does the following three things:
1, name search. First name lookup is performed in the visible name entity in the compilation unit where it is located. (1) The class member function takes precedence (the class that the object resides in, and the base class). Stop the lookup once it is found. (2) If not, do a further search in the appropriate namespace; (3) If not, it will be looked up according to the namespace in which the function parameter resides (keoning lookup).
2, overload resolution. The overload resolution is based on the name found, and the corresponding function is selected according to the principle of the parameter matching.
3, accessibility check. Used to determine whether the selected function can be called.
Description
1) According to the first article, it is obvious that if a type wants to work with a non-member function, they should be placed in the same namespace. For example, a generic type of overloaded operators and parameter types are placed in the same header file/or under the same namespace.
2) The function special template does not participate in overload resolution, so if you want to use a function of the special, the best way is to overload the function, in the implementation of the use of this special to work.
3) Overload resolution occurs before the accessibility check. Therefore, if the private function unfortunately participates in the overload and is selected, there will eventually be an unreachable compilation hint. This often implies ambiguity, and the design itself is unreasonable. In other words, private parameters are not "private" when the name is searched and overloaded.
The rules in the article are very important, and after understanding, many C + + compiler problems will be solved naturally. There will be further articles on how to determine the visible names in each C + + compilation unit (CPP file), which are related to the name lookup, the keoning lookup, the overloading (overload), and the implementation of the virtual function (override). To learn more programming language Tutorials Please login e mentor Web.
Compile process using the C + + compiler