answersLogoWhite

0

disadvantage of friend functions is that they require an extra line

of code when you want dynamic binding. To get the effect of a virtual friend,

the friend function should call a hidden (usually protected:) virtual[20]

member function.

User Avatar

Wiki User

17y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
More answers

All class member functions have the following three qualities:

1. Private access to the class.

2. Scoped to the class.

3. Invoked upon an object of the class.

Static member functions only have the first two qualities while friend functions have the first quality only.

The merit of a friend function is that you can grant private access to the class without having to expose more of the class than you actually need to.

The demerit of a friend function is that if a suitable public interface already exists, then you grant far more access to the function than you need to.

User Avatar

Wiki User

10y ago
User Avatar

A friend function is an external function that cannot logically be declared a member of the class but that requires private access to that class. For instance, if a function is a member of one class then it cannot be declared a member of another class -- but the other class can still declare it a friend.

Since friend functions are not actually members of the class, they have no implicit this pointer. In that respect they are not unlike static member functions. Indeed, once you declare a function to be a friend of a class, you must treat it just as if it were a static member of the class.

The merits of a friend function depend on whether friendship is actually required or not. For instance, when an external function requires private access you basically have two options available:

  1. create a public interface that meets the needs of the function.
  2. declare the function to be a friend of the class and leave the interface unchanged.

The problem with the first option is that if you expose a public interface to the external function, then you expose that same interface to all external functions. If that interface has potential to undermine encapsulation then the latter option would be the preferred choice. Although you expose far more to the external function than it may require, the exposure is limited to that one function.

Remember that although a friend function is not a member of the class, it must be treated just as if it were a static member of the class. Its implementation must follow the same rules of encapsulation that any other member of the class must adhere to. This means that you -- the class developer -- must be in full control of the external function's implementation. If you have no control over the external function's implementation then the external function must not be declared a friend and you must expose a public interface instead.

User Avatar

Wiki User

11y ago
User Avatar

Friend functions and friend classes have privileged access to a class' private and protected members, extending your class' interface, making the friend part of that interface. When two classes work closely together as a unit, friend functions are almost a necessity. The only downside is that you cannot narrow their scope within a class, but you can always limit which classes you make friends with and design your interfaces accordingly. Keep in mind that friendship cannot be inherited and, ultimately, you are in control of who is a friend, and who is a stranger.

Novice programmers will often claim friends to be the work of the devil, undermining the data encapsulation that lies at the heart of object-oriented programming. And yet they will have no qualms regarding public derivation. Go figure.

User Avatar

Wiki User

12y ago
User Avatar

You use a friend function when the function needs access to the private and/or protected members of the class within which it is declared. The demerit is that it exposes the entire class to the function, however this is no worse than using public inheritance. It is a necessity to use friend functions when two classes must work together, or where an external function is required to reverse an assignment operator. For instance, you could assign an integer to your object, but you can't assign your object to an integer unless you use a friend function, because the return value must be an integer, not your object.

User Avatar

Wiki User

12y ago
User Avatar

merit

We can able to access the other class member in our class

Demerit

Maximum size of memory will occupied by object accord. To the size of the friend member

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What are the merits and demerits of using friend function?
Write your answer...
Submit
Still have questions?
magnify glass
imp