Page 115 - DCAP404 _Object Oriented Programming
P. 115
Object-oriented Programming
Notes class C;
class A {
class B { // arbitrary nested class definitions
friend class C;
};
};
If the friend function is a member of another class, you need to use the scope resolution operator
(::).
Example:
class A {
public:
int f() { }
};
class B {
friend int A::f();
};
Friends of a base class are not inherited by any classes derived from that base class. The following
example demonstrates this:
class A {
friend class B;
int a;
};
class B { };
class C : public B {
void f(A* p) {
// p->a = 2;
}
};
The compiler would not allow the statement p->a = 2 because class C is not a friend of class A,
although C inherits from a friend of A.
Friendship is not transitive. The following example demonstrates this:
class A {
friend class B;
int a;
};
class B {
friend class C;
108 LOVELY PROFESSIONAL UNIVERSITY