>can a class allow a friend method of a friend class to use it's protected or private members
Just because you're my friend doesn't mean your other friends are also my friends.
>can a friend method of a derived class use base class's protected members
If you're my child, your friends aren't necessarily my friends.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>> can a friend method of a derived class use base class's protected members
friend functions have the same access rights as the members of the class. a derived class can access (only) inherited protected members of the base class. if a derived class can access the base class's protected members, friends of the derived class can access them too. and if a derived class cannot access the base class's protected members (because it has not inherited them), friends of the derived class also cannot access them. for example:
struct base_class
{
protected: int protected_member ;
};
struct derived_class : base_class
{
void member_function( derived_class& derived, base_class& base )
{
derived.protected_member = 9 ; // ok, inherited by derived_class
derived.base_class::protected_member = 9 ; // also ok, inherited
base.protected_member = 9 ; // error, not inherited by derived_class
}
friend inline void friend_function( derived_class& derived,
base_class& base )
{
derived.protected_member = 9 ; // ok, inherited by derived_class
derived.base_class::protected_member = 9 ; // also ok, inherited
base.protected_member = 9 ; // error, not inherited by derived_class
}
};
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287