Page 113 - DCAP404 _Object Oriented Programming
P. 113
Object-oriented Programming
Notes // friendFunction() is a not a member function of any class.
int friendFunction(MyClass x)
{
/* Because friendFunction() is a friend of MyClass, it can
directly access a and b. */
int max = x.a < x.b ? x.a : x.b;
return max;
}
int main()
{
MyClass n(18, 111);
cout << “friendFunction(n) is “ << friendFunction(n) << “\n”;
return 0;
}
friendFunction(n) is 18
5.4.2 Friend Function in Nested Classes
Friend functions declared in a nested class are considered to be in the scope of the nested class,
not the enclosing class. Therefore, the friend functions gain no special access privileges to
members or member functions of the enclosing class. If you want to use a name that is declared
in a nested class in a friend function and the friend function is defined in file scope, use qualified
type names as follows:
// friend_functions_and_nested_classes.cpp
#include <string.h>
char *rgszMessage[255];
class BufferedIO
{
public:
class BufferedInput
{
public:
friend int GetExtendedErrorStatus();
static char *message;
int iMsgNo;
};
};
char *BufferedIO::BufferedInput::message;
int GetExtendedErrorStatus()
106 LOVELY PROFESSIONAL UNIVERSITY