• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1559
  • Last Modified:

C++ Friend -- Friend Class - Can I make all its sub-classes also friends automatically?

Class A is a master "grandparent" class.
Class B inherits from Class A.
Class C inherits from Class B.

Class Tester{
private:
  int private_int;
  friend class A;
}

Is there some way to give Classes B and C access to Class Tester's private members without also including "friend class B;" and "friend class C;"?

I would like the subclasses of Class A to inherit Class A's friend status with other classes.

Thanks in advance for your time.
0
oxygen_728
Asked:
oxygen_728
  • 2
  • 2
1 Solution
 
jkrCommented:
No, you need to explicitly declare each 'friend'. See also http://www.cplusplus.com/doc/tutorial/inheritance.html ("Friendship and inheritance"): "Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified."

"C++ is where friends have access to your privates" ;o)
0
 
oxygen_728Author Commented:
I think it is completely ridiculous that there isn't a transitive version.

Thanks for the info jkr, I don't think you're ridiculous =)
0
 
oxygen_728Author Commented:
jkr is my hero
0
 
jkrCommented:
Actually, it is fine the way it is. IMO friends should be used really sparsely, since it to some extent breaks OO principles. Yet sometimes you won't be able to completely avoid it.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now