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

Dani AI

Generated

Short answer: no — friendship does not chain or get inherited. summed it up well with the friends-of-friends analogy. If ClassA makes ClassB a friend, that does not automatically give any third function or class that happens to be a member of B access to A’s private/protected members unless A explicitly grants that access.

Clarifications and how to grant access

  • To give a single member function of another class access, the declaring class must explicitly friend that member (you can friend a specific qualified member or the whole class). That friend declaration is the only thing that grants access — not other friendships or inheritance. Be aware of declaration-order rules when you friend a qualified member; in practice it’s often simpler to friend the whole class or add a small public/protected accessor instead.
  • Regarding protected members and derived classes: a derived-class member (and friends of the derived class) can access base-class protected subobjects only when the object being accessed is of the derived type (or further derived). Trying to access a base object’s protected member through a base-typed lvalue from a derived/friend context is a common pitfall.

Practical advice

  • Prefer minimal, explicit friendship (or narrow accessors) to avoid tight coupling.
  • If the intent is a single operation from an unrelated class, friend that exact function; if many members need access, friend the class.
  • See the standard-style rules and examples for exact wording and corner cases on cppreference: friend declarations and .

This expands on ’s illustration of the protected-access gotcha and ties back to ’s point about non-transitive friendship.

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.