answersLogoWhite

0


Best Answer

C is not object oriented programming language thus there are no friend functions in C.

In C++, which is object oriented, the advantage of a friend is that it allows code that cannot otherwise be regarded as being a member of the class to gain private access to the class representation, where public access to the representation would be considered undesirable. Although many see friendship as an undermining of encapsulation, the reality is it strengthens encapsulation by limiting exposure to the representation to only those functions that actually require that access. Public access is fully accessible, of course, but is not always desirable. Interfaces must be kept as simple as possible, but exposing an interface that is only of use to a few non-member functions is less desirable than simply making those functions friends of the class.

It is important to understand the relationships between functions and classes. All functions can be divided into one of three distinct categories with respect to any one class: non-member functions; static member functions or; nonstatic member functions.

Nonstatic member functions of a class have all three of the following properties:

1. The function has private access to the class representation.

2. The function is scoped to the class.

3. The function must be invoked upon an object of the class (has a 'this' pointer).

Static member functions only have the first two properties while friend functions only have the first property. External (non-friend) functions do not have any of these properties.

The only disadvantage to a friend function is when that friend is outwith the control of the class designer. Since friends have private access they must obey the class invariants, just as any member of the class must. Although they are not members of the class per se, they must be implemented just as if they were. In other words, they must not invalidate the representation. When the class designer has full control over the friends of that class then this is not a major problem. But when third-party programmers have access to the friend implementations, encapsulation can no longer be guaranteed by the class designer -- it is seriously undermined. But when used appropriately, friends actively re-enforce encapsulation and are no more an undermining of encapsulation than is public inheritance.

User Avatar

Wiki User

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

Wiki User

14y ago

A friend function can use any of the private data of an object without going thru a mutator or gateway function. As a result, since it can do anything it wants with the private data of an object there is no built-in checking on validity, etc.

But, then again, that is what the friend function is for. An old saying revolving around this is that you need to trust your friends, and your friend better be a 'very good friend' to you (the object class).

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are no disadvantages as such. Some will tell you at great length that friends undermine encapsulation but this is errant nonsense. Friends no more undermine encapsulation than do members of the class itself, and friends are effectively members of the class. They extend the class interface, nothing more.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The only disadvantage is misuse. When used correctly, friend functions extend a class interface. But if a friend function undermines encapsulation, then there is a clear design flaw in the class, and the use of a friend function should be reconsidered.

It coincide the concept of data hiding

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

OOPS is learn to hard and not easy to new developer.

This answer is:
User Avatar

User Avatar

bihar computer netwo...

Lvl 2
3y ago

No

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Disadvantages of friend function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the code to perform friend function in C plus plus?

Something like this:


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


What are the limitations of friend function in c plus plus?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.