Modifying a Class’s private Data with a Friend Function

Figure 9.22 is a mechanical example in which we define friend function setX to set the private data member x of class Count. As a convention, we place the friend declaration (line 9) first in the class definition, even before public member functions are declared. Again, this friend declaration can appear anywhere in the class.


 1   //Fig. 9.22: fig09_22.cpp
 2   // Friends can access private members of a class.
 3   #include <iostream>
 4   using namespace std;
 5
 6   // Count class definition
 7   class Count
 8   {
 9      friend void setX( Count &, int ); // friend declaration
10   public:
11      // constructor
12      Count()
13         : x( 0 ) // initialize x to 0
14      {
15         // empty body
16      } // end constructor Count
17
18      // output x
19      void print() const
20      {
21         cout << x << endl;
22      } // end function print
23   private:
24      int x; // data member
25   }; // end class Count
26
27   // function setX can modify private data of Count         
28   // because setX is declared as a friend of Count (line 9) 
29   void setX( Count &c, int val )                            
30   {                                                         
31      c.x = val; // allowed because setX is a friend of Count
32   } // end function setX                                    
33
34   int main()
35   {
36      Count counter; // create Count object
37
38      cout << "counter.x after instantiation: ";
39      counter.print();
40
41      setX( counter, 8 ); // set x using a friend function
42      cout << "counter.x after call to setX friend function: ";
43      counter.print();
44   } // end main


counter.x after instantiation: 0
counter.x after call to setX friend function: 8


Fig. 9.22. Friends can access private members of a class.

Function setX (lines 29–32) is a stand-alone (global) function—it isn’t a member function of class Count. For this reason, when setX is invoked for object counter, line 41 passes counter as an argument to setX rather than using a handle (such as the name of the object) to call the function, as in

counter.setX( 8 ); // error: setX not a member function

If you remove the friend declaration in line 9, you’ll receive error messages indicating that function setX cannot modify class Count’s private data member x.

As we mentioned, Fig. 9.22 is a mechanical example of using the friend construct. It would normally be appropriate to define function setX as a member function of class Count. It would also normally be appropriate to separate the program of Fig. 9.22 into three files:

1. A header (e.g., Count.h) containing the Count class definition, which in turn contains the prototype of friend function setX

2. An implementation file (e.g., Count.cpp) containing the definitions of class Count’s member functions and the definition of friend function setX

3. A test program (e.g., fig09_22.cpp) with main.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37