C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
Friend Functions
                                             Up Contents Index Back
─────C/C++ Language─────────────────────────────────────────────────────────
 
     A friend function is a function that is not a member of a class but
     has access to the class's private and protected members.
 
     A friend function is declared by the class that is granting access.
     For example:
 
         class Complex
         {
         public:
             Complex( float re, float im );
             friend Complex operator+( Complex first, Complex second );
         private:
             float real, imag;
         };
 
     The friend declaration can be placed anywhere in the class
     declaration. It is not affected by the access control keywords.
 
     A friend function is defined as a nonmember function. For example:
 
         Complex operator+( Complex first, Complex second )
         {
             return Complex( first.real + second.real,
                             first.imag + second.imag );
         }
 
     In this example, the friend function operator+ has access to the
     private data members of the Complex objects it receives as
     parameters.
 
     Notice that the friend keyword does not appear in the function
     definition.
                                    -♦-