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.
Chat with our AI personalities
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.
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:
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.
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.
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.
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
Write a merits and demerits of using function in program
The biggest merit of HTML to develop a website is that it is required. You can not display a website without using some type of HTML, even when developing complex .NET pages.
It requires the same amount of memory, so it wouldn't spare anything. Don't do it.
Explain The merits of using a deque to implement a stack in data structure
disadvantage of friend functions is that they require an extra lineof 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.Read more: Demerits_of_friend_function