title was self explanatory..

i repeat ..

can a class allow a friend method of a friend class to use it's protected or private members

oh yes one more,

can a friend method of a derived class use base class's protected members

i am new to this type of concept
please help

thanks in advance

Recommended Answers

All 2 Replies

>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.

>> 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
  }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.