Login
Register
All Activity
Questions
Unanswered
Tags
Books
@ProAnswers
Enter your email address:
C Books Guide and List
C++ Books Guide and List
Best Java Books
What are advantages/disadvantages of using friends in c++?
+1
vote
c++
asked
by
anonymous
edited
by
pa
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
by
anonymous
Related questions
+1
vote
1
answer
what are advantages of pointers in C?
asked
by
sunta
Newbie
(
160
points)
0
votes
1
answer
what are destructors in c++
asked
by
anonymous
c++
0
votes
1
answer
What are the drawbacks of using pointers in C?
asked
by
carlton
Sr Member
(
3,990
points)
c
c-language
+1
vote
1
answer
What is the size of double variable in C and C++ language?
asked
by
anonymous
c
c++
c-language
cpp
c-program
+1
vote
1
answer
what are advantages of C over C++?
asked
by
anonymous
c-language
cpp