Names and Qualified Names

Names used with the binary scope-resolution operator (::) are called “qualified names.” The name specified after the binary scope-resolution operator must be a member of the class specified on the left of the operator or a member of its base class(es).

Names specified after the member-selection operator (. or –>) must be members of the class type of the object specified on the left of the operator or members of its base class(es). Names specified on the right of the member-selection operator (–>) can also be objects of another class type, provided that the left-hand side of –> is a class object and that the class defines an overloaded member-selection operator (–>) that evaluates to a pointer to some other class type. (This provision is discussed in more detail in Class Member Access in Chapter 12.)

The compiler searches for names in the following order, stopping when the name is found:

  1. Current block scope if name is used inside a function; otherwise, global scope.

  2. Outward through each enclosing block scope, including the outermost function scope (which includes function arguments).

  3. If the name is used inside a member function, the class’s scope is searched for the name.

  4. The class’s base classes are searched for the name.

  5. The enclosing nested class scope (if any) and its bases are searched. The search continues until the outermost enclosing class scope is searched.

  6. Global scope is searched.

However, you can make modifications to this search order as follows:

  1. Names preceded by :: force the search to begin at global scope.

  2. Names preceded by the class, struct, and union keywords force the compiler to search only for class, struct, or union names.

  3. Names on the left side of the scope-resolution operator (::) can be only class, struct, or union names.

If the name refers to a nonstatic member but is used in a static member function, an error message is generated. Similarly, if the name refers to any nonstatic member in an enclosing class, an error message is generated because enclosed classes do not have enclosing-class this pointers.