954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can a class grant access to a friend method of friend class?

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

tnvkrishna
Light Poster
32 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

This question has already been solved

Post: Markdown Syntax: Formatting Help
You