C++ Class Construct  «Prev  Next»
Lesson 2 Scope resolution operator
Objective This page describes the unary and binary forms of the scope resolution operator in C++

C++ Scope Resolution Operator

The class construct adds a new set of scope rules to those you are already familiar with in C++. One point of classes is to provide an encapsulation technique. Conceptually, it makes sense that all names declared within a class be treated as if they were in their own namespace, distinct from external names, function names, and other class names. This creates a need for the scope resolution operator (::). The scope resolution operator is the highest precedence operator in the C++ language. It comes in two forms: unary and binary. We will examine these forms in the next two lessons.

The scope resolution operator helps to identify and specify the context to which an identifier refers. The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .h extension) of a particular class. In the .cpp file one can define the usual global functions or the member functions of the class. To distinguish between the normal functions and the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name , for example

where Account is a class and getBalance() is a member function of the class Account.
The other uses of the resolution operator is to resolve the scope of a variable when the same identifier is used to represent a
  1. global variable,
  2. a local variable, and
  3. members of one or more class(es).

If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is referenced. If the resolution operator is placed in front of the variable name then the global variable is referenced. When no resolution operator is placed then the local variable is referenced.

#include <iostream>
using namespace std;
int n = 12;   // A global variable

int main() {
   int n = 13;   // A local variable
   cout  << ::n << endl;  // Print the global variable: 12
   cout  << n   << endl;  // Print the local variable: 13

Member Functions Defined Outside the Class

So far we have seen member functions that were defined inside the class definition. This need not always be the case. ENGLCON shows a member function, add_dist(), that is not defined within the Distance class definition. It is only declared inside the class, with the statement

void add_dist( Distance, Distance );

This tells the compiler that this function is a member of the class but that it will be defined outside the class declaration, someplace else in the listing. The add_dist() function is defined following the class definition. //
//add lengths d2 and d3
void Distance::add_dist(Distance d2, Distance d3){
 inches = d2.inches + d3.inches; //add the inches
 feet = 0; //(for possible carry)
 if(inches >= 12.0) //if total exceeds 12.0,{ //then decrease inches
  inches -= 12.0; //by 12.0 and
  feet++; //increase feet
 } //by 1
 feet += d2.feet + d3.feet; //add the feet

The declarator in this definition contains some unfamiliar syntax. The function name, add_dist(), is preceded by the class name, Distance, and a new symbol, the double colon (::). This symbol is called the scope resolution operator. It is a way of specifying what class something is associated with. In this situation, Distance::add_dist() means the add_dist() member function of the Distance class. Figure 3.2 shows its usage.
Figure 3.2 The scope resolution operator.
Figure 3.2 The scope resolution operator.

Thinking in C++