Member Scope

Member functions and static members can be defined outside their class declaration if they have already been declared, but not defined, in the class member list. Nonstatic data members are defined when their class is instantiated. The declaration of a static data member is not a definition. The declaration of a member function is a definition if the body of the function is also given.

Whenever the definition of a class member appears outside of the class declaration, the member name must be qualified by the class name using the :: ( scope resolution) operator.

All member functions are in class scope even if they are defined outside their class declaration.

The name of a class member is local to its class. Unless you use one of the class access operators, . (dot) , or -> (arrow), or :: (scope resolution) operator, you can only use a class member in a member function of its class and in nested classes. You can only use types, enumerations and static members in a nested class without qualification with the :: operator.

The order of search for a name in a member function body is:

  1. Within the member function body itself
  2. Within all the enclosing classes, including inherited members of those classes
  3. Within the lexical scope of the body declaration

Note: When the containing classes are being searched , only the definitions of the containing classes and their base classes are searched. The scope containing the base class definitions (global scope, in this example) is not searched.



Scope in C++


Example of Member Function Search Path
Example of Defining a Member outside of the Class


Scope Resolution Operator
Member Functions
Dot Operator
Arrow Operator
Member Access