Class Scope and Accessing Class Members

A class's data members (variables declared in the class definition) and member functions (functions declared in the class definition) belong to that class's scope. Nonmember functions are defined at file scope.

Within a class's scope, class members are immediately accessible by all of that class's member functions and can be referenced by name. Outside a class's scope, public class members are referenced through one of the handles on an objectan object name, a reference to an object or a pointer to an object. The type of the object, reference or pointer specifies the interface (i.e., the member functions) accessible to the client. [We will see in Chapter 10 that an implicit handle is inserted by the compiler on every reference to a data member or member function from within an object.]



Member functions of a class can be overloaded, but only by other member functions of that class. To overload a member function, simply provide in the class definition a prototype for each version of the overloaded function, and provide a separate function definition for each version of the function.

Variables declared in a member function have block scope and are known only to that function. If a member function defines a variable with the same name as a variable with class scope, the class-scope variable is hidden by the block-scope variable in the block scope. Such a hidden variable can be accessed by preceding the variable name with the class name followed by the scope resolution operator (::). Hidden global variables can be accessed with the unary scope resolution operator (see Chapter 6).

The dot member selection operator (.) is preceded by an object's name or with a reference to an object to access the object's members. The arrow member selection operator (->) is preceded by a pointer to an object to access the object's members.

Figure 9.4 uses a simple class called Count (lines 825) with private data member x of type int (line 24), public member function setX (lines 1215) and public member function print (lines 1821) to illustrate accessing the members of a class with the member selection operators. For simplicity, we have included this small class in the same file as the main function that uses it. Lines 2931 create three variables related to type Countcounter (a Count object), counterPtr (a pointer to a Count object) and counterRef (a reference to a Count object). Variable counterRef refers to counter, and variable counterPtr points to counter. In lines 3435 and 3839, note that the program can invoke member functions setX and print by using the dot (.) member selection operator preceded by either the name of the object (counter) or a reference to the object (counterRef, which is an alias for counter). Similarly, lines 4243 demonstrate that the program can invoke member functions setX and print by using a pointer (countPtr) and the arrow (->) member selection operator.

Figure 9.4. Accessing an object's member functions through each type of object handlethe object's name, a reference to the object and a pointer to the object.

(This item is displayed on page 490 in the print version)

1 // Fig. 9.4: fig09_04.cpp 2 // Demonstrating the class member access operators . and -> 3 #include 4 using std::cout; 5 using std::endl; 6 7 // class Count definition 8 class Count 9 { 10 public: // public data is dangerous 11 // sets the value of private data member x 12 void setX( int value ) 13 { 14 x = value; 15 } // end function setX 16 17 // prints the value of private data member x 18 void print() 19 { 20 cout << x << endl; 21 } // end function print 22 23 private: 24 int x; 25 }; // end class Count 26 27 int main() 28 { 29 Count counter; // create counter object 30 Count *counterPtr = &counter; // create pointer to counter 31 Count &counterRef = counter; // create reference to counter 32 33 cout << "Set x to 1 and print using the object's name: "; 34 counter.setX( 1 ); // set data member x to 1 35 counter.print(); // call member function print 36 37 cout << "Set x to 2 and print using a reference to an object: "; 38 counterRef.setX( 2 ); // set data member x to 2 39 counterRef.print(); // call member function print 40 41 cout << "Set x to 3 and print using a pointer to an object: "; 42 counterPtr->setX( 3 ); // set data member x to 3 43 counterPtr->print(); // call member function print 44 return 0; 45 } // end main  

Set x to 1 and print using the object's name: 1 Set x to 2 and print using a reference to an object: 2 Set x to 3 and print using a pointer to an object: 3  

Категории