Friend Classes. McGraw-Hill The Complete Reference Fourth Edition
ಜಾಹಿರಾತು
ಜಾಹಿರಾತು
302
C + + : T h e C o m p l e t e R e f e r e n c e
C1 x;
C2 y; x.set_status(IDLE); y.set_status(IDLE); if(x.idle(y)) cout << "Screen can be used.\n"; else cout << "In use.\n"; x.set_status(INUSE); if(x.idle(y)) cout << "Screen can be used.\n"; else cout << "In use.\n";
} return 0;
Because idle( ) is a member of C1, it can access the status variable of objects of type
C1
directly. Thus, only objects of type C2 need be passed to idle( ).
There are two important restrictions that apply to friend functions. First, a derived class does not inherit friend functions. Second, friend functions may not have a storage-class specifier. That is, they may not be declared as static or extern.
Friend Classes
It is possible for one class to be a friend of another class. When this is the case, the
friend
class and all of its member functions have access to the private members defined within the other class. For example,
// Using a friend class.
#include <iostream> using namespace std; class TwoValues { int a; int b; public:
};
TwoValues(int i, int j) { a = i; b = j; } friend class Min; class Min {
ಡೌನ್ಲೋಡ್ ಮಾಡಿ
ಜಾಹಿರಾತು