search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

1 vote
16.1k views
asked anonymous
edited by

1 Answer

1 vote
 
Best answer
Advantages:
 using friends is generally syntactic. Ie: both a member fn
and a friend are equally privileged

a friend function can be called like f(obj), where a member is called like obj.f().

friends are used when two or more classes are designed to be more tightly coupled than you want for.

Disadvantages:
they add to the global namespace. In contrast, the namespace of member functions is buried within the class, reducing the chance for namespace collisions for functions.

They aren't inherited. That is, the `friendship privilege' isn't inherited. This is actually an advantage when it comes to encapsulation. Ex: I may declare you as my friend, but that doesn't mean I trust your kids.

they don't bind dynamically. Ie: they don't respond to polymorphism. There are no virtual friends; if you need one, have a friend call a hidden (usually `protected:') virtual member fn. Friends that take a ptr/ref to a class can also take a ptr/ref to a publically derived class object, so they act as if they are inherited, but the friendship *rights* are not inherited
answered anonymous

Related questions

0 votes
1 answer 91 views
91 views
asked keem Expert 13.2k points 27 40 59
0 votes
1 answer 280 views
0 votes
1 answer 75 views
0 votes
1 answer 169 views
0 votes
1 answer 377 views
...