OOPortal 





Designing Reusable Code  «Prev  Next»
Lesson 4 Friend functions
Objective How and where to declare a friend function.
Before continuing our exploration of operator overloading, we need to master the use of friend functions.
A friend function gives a nonmember function access to the hidden members of the class and must be declared inside the class declaration to which it is a friend. The function is prefaced by the keyword friend and can appear in any part of the class without affecting its meaning.
Our style in this course is to place the friend declaration in the public part of the class. Since access has no effect on the friend declaration, friend functions are conceptually public.
Member functions of one class can be friend functions of another class. In this case, they are declared in the class to which they are a friend, using the scope resolution operator to qualify its function name.
If all member functions of one class are friend functions of a second class, this can be specified by using the general form:
friend class class name.
The following declarations illustrate this syntax:

class window {
   .....
   friend void refresh(window w);        //friend
   friend void terminal::draw(window w); //friend
};

class node {
   .....
   friend class tree;
   //tree members have access to node
};
Friend Function - Quiz
Click the Quiz link below to take a brief multiple-choice quiz on friend functions.
Friend Function - Quiz

Friends

C++ allows classes to declare that other classes or nonmember functions are friends, and can access 1) protected and 2) private data members and methods. For example, the SpreadsheetCell class could specify that the Spreadsheet class is its “friend” like this:
class SpreadsheetCell
{
public:
friend class Spreadsheet;
// Remainder of the class omitted for brevity
};
Now all the methods of the Spreadsheet class can access the private and protected data and members of the SpreadsheetCell class. Similarly, you can specify that one or more functions or members of another class are friends. For example, you might want to write a function to verify that the value and the string of a SpreadsheetCell object are really in synch. You might want this verification routine to be outside the SpreadsheetCell class to model an external audit, but the function should be able to access the internal data members of the object in order to check it properly. Here is the SpreadsheetCell class definition with a friend checkSpreadsheetCell() function:

class SpreadsheetCell{
public:
// Omitted for brevity
friend bool checkSpreadsheetCell(const SpreadsheetCell &cell);
// Omitted for brevity
};
The friend declaration in the class serves as the function’s prototype. There is no need to write the prototype elsewhere (although it’s harmless to do so). Here is the function definition:
bool checkSpreadsheetCell(const SpreadsheetCell &cell)
{
return (SpreadsheetCell::stringToDouble(cell.mString) == cell.mValue);
}
You write this function just like any other function, except that you can directly access private and protected data members of the SpreadsheetCell class. You do not repeat the friend keyword on the function definition.
friend classes and methods are easy to abuse; they allow you to violate the principle of abstraction by exposing internals of your class to other classes or functions. Thus, you should use them only in limited circumstances such as operator overloading.