answersLogoWhite

0


Best Answer

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

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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

This answer is:
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
Related questions

What r the Demerits of function in C?

Write a merits and demerits of using function in program


What are the merits and demerits of using function in c?

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


What are the disadvantages and advantages of using blekko search engine?

blekko merits and demerits


What are the Merits and Demerits of using HTML?

The merit of HTML is it's simplicity. The demerit is the lack of conditional statements.


What are the merits and demerits of recursion?

Ans: Merits of recursion are: Mathematical functions, such as Fibonacci series generation can be easily implemented using recursion as compared to iteration technique. Demerits of recursion are: Many programming languages do not support recursion; hence, recursive mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time and memory space. The recursive programs take considerably more storage and take more time during processing.


What are the advantage and disadvantage of handphones for students?

Well, if they have something at school you have to do at short notice, you could call their parents or something to tell them, and you don't have to ride the bus or whatever. But you could accidentally get a text from somebody during class and fyou could get in trouble, so... dont turn it on in class.


Merits and demerits of calculator?

Using a calculator can be beneficial because it helps compute math problems very quickly. The downside is that the batteries or power may go out when you need to use it.


What are the merits and demerits of using HTML to develop a website?

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.


What are merits and demerits of using Facebook?

Merits: Facebook is a powerful platform for connecting with others, sharing information, and promoting businesses. It allows users to stay in touch with friends and family regardless of distance. Demerits: Privacy concerns, spread of misinformation, and addiction are some of the potential drawbacks of using Facebook. The platform has also faced criticism for its impact on mental health and its role in spreading fake news.


What benefits and demerits do embedded style sheet have?

Merits of Embedded Style Sheets: Multiple tag types can be created in a single document. Styles, in complex situations, can be applied by using Selector and Grouping methods. Extra download is unnecessary. Demerits of Embedded Style Sheets: Multiple documents cannot be controlled.


Would there be any Memory issues or execution issues or any merits or demerits of using 1D array of size 'mn' instead of using 2D array of size mxn?

It requires the same amount of memory, so it wouldn't spare anything. Don't do it.


What are the demerits of using pesticides?

it is very harmful to others