14.1 Friends

I l @ ve RuBoard

14.1 Friends

In Chapter 13, you defined a basic stack class. Suppose you want to write a function to see whether two stacks are equal. At first glance this is simple. The function looks like Example 14-1.

Example 14-1. stack_c/s_equal.cpp
 /********************************************************  * stack_equal -- Test to see if two stacks are equal   *  *                                                      *  * Parameters                                           *  *      s1, s2 -- the two stacks                        *  *                                                      *  * Returns                                              *  *      0 -- stacks are not equal                       *  *      1 -- stacks are equal                           *  ********************************************************/ int stack_equal(const stack& s1, const stack& s2) {     int index;  // Index into the items in the array     // Check number of items first     if (s1.count != s2.count)         return (0);     for (index = 0; index < s1.count; ++index) {         assert((index >= 0) &&                 (index < sizeof(s1.data)/sizeof(s1.data[0])));         assert((index >= 0) &&                 (index < sizeof(s2.data)/sizeof(s2.data[0])));         if (s1.data[index] != s2.data[index])             return (0);     }     return (1); } 

Like many programs, this solution is simple, clear, and wrong . The problem is that the member variables count and data are private. That means you can't access them.

So what do you do? One solution is to make these variables public. That gives the function stack_equal access to count and data . The problem is that it also gives everyone else access, and you don't want that.

14.1.1 Friend Functions

Fortunately C++ gives you a way to say, "Let stack_equal and only stack_equal have access to the private data of the class stack ." This is accomplished through the friend directive. Classes must declare their friends. No function from the outside may access the private data from the class, unless the class allows it.

Example 14-2. stack_c/f_stack.cpp
 // The stack itself class stack {     private:         int count;              // Number of items in the stack         int data[STACK_SIZE];   // The items themselves     public:         // Initialize the stack         void init(  );         // Push an item on the stack         void push(const int item);         // Pop an item from the stack         int pop(  );         friend int stack_equal(const stack& s1, const stack& s2); }; 

stack_equal is not a member function of the class stack . It is a normal, simple function. The only difference is that because the function is a friend , it has access to private data for any class that calls it a friend.

14.1.2 Friend Classes

Friends are not restricted to just functions. One class can be a friend of another. For example:

 class item {     private:         int data;     friend class set_of_items; }; class set_of_items {     // ... }; 

In this case, since the class set_of_items is a friend of item , it has access to all the members of item .

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net