1

I am not exactly sure what the use of a friend function or class is when you can easily just use inheritance (parent/child classes)? From my understanding, a friend function or class allows a non-member function to access the members of another class that it is declared a friend of. Similarly, a child class can access the members of a parent class. I am wondering, in which situation would you pick one over the other?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
K.Patel
  • 53
  • 1
  • 1
  • 4
  • These things (inheritance and `friend`) are not related. You would use whichever solves the problem at hand. They rarely overlap. – François Andrieux May 12 '17 at 18:35
  • 2
    IMHO duplicate link must be: http://stackoverflow.com/questions/18085169/whats-the-difference-between-friendship-and-inheritance. – chema989 May 12 '17 at 18:39

1 Answers1

1

Because you may want to use some private functions/member of a class without being a child of it....

You should read more about inheritance. You can't consider inheritance as a solution to access "members of a class" as you mentioned.

jpo38
  • 20,821
  • 10
  • 70
  • 151