944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5554
  • C++ RSS
Nov 7th, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tnvkrishna is offline Offline
32 posts
since Oct 2007
Nov 7th, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2007
0

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

>> 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:
c++ Syntax (Toggle Plain Text)
  1. struct base_class
  2. {
  3. protected: int protected_member ;
  4. };
  5.  
  6. struct derived_class : base_class
  7. {
  8. void member_function( derived_class& derived, base_class& base )
  9. {
  10. derived.protected_member = 9 ; // ok, inherited by derived_class
  11. derived.base_class::protected_member = 9 ; // also ok, inherited
  12. base.protected_member = 9 ; // error, not inherited by derived_class
  13. }
  14. friend inline void friend_function( derived_class& derived,
  15. base_class& base )
  16. {
  17. derived.protected_member = 9 ; // ok, inherited by derived_class
  18. derived.base_class::protected_member = 9 ; // also ok, inherited
  19. base.protected_member = 9 ; // error, not inherited by derived_class
  20. }
  21. };
Last edited by vijayan121; Nov 8th, 2007 at 3:51 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help reading and writing to file
Next Thread in C++ Forum Timeline: Complete Noob. Reading/Writing to/from Textfile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC