answersLogoWhite

0


Best Answer

A normal member function has the following qualities:

1. Has private access to the class.

2. Is scoped to the class.

3. Can be invoked on an instance of the class.

Static functions have the first two qualities only. Friend functions have the first quality only.

It therefore follows that friend functions are only compulsory when you need quality 1 only.

User Avatar

Wiki User

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

Wiki User

7y ago

A friend function is necessary whenever a non-member function of a class requires private access to that class. Any function can obviously gain private access to a class simply by being declared a member of that class, however when a function requires private access to two or more classes it cannot possibly be a member of them all. In that case it must either be declared a friend of them all or be declared a member of one and a friend of all others. More importantly, just because a function requires private access to one class it does not follow that it should become a member of that class. For instance, many operators are defined as non-member functions and if we wish to take advantage of argument-dependent lookup we must define them in similar terms. It therefore follows that where an operator overload requires private access to the class upon which it operates, then we must declare it a friend of that class. The stream insertion and extraction operators are typical examples.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is a friend function compulsory?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


What is the latent function of friends?

The only function of a friend is to extend the private class interface outwith the class, essentially making the friend part of the class interface.


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


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.


Does java supports friend function?

I don't believe that Excel has such a function; you'll have to write one yourself.

Related questions

What are the compulsory function of municipal corporation?

ddsseew


What is the difference between constituent functions and ministrant function?

Compulsory in nature


What do you do if your best friend isn't a Christian?

There is no need to do anything. It is not compulsory to be a Christian.


What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


Explain the advantages and disadvantages of friend functions?

by: THE DJ AKwww.the-dj-ak.webs.comwww.thedjak.co.nrwww.thedjak.webs.comWhat is a Friend Function?A friend function is a special function in c++ which inspite of not being member fuctionof a class has privalage to access private and protected data of a class.A friend function is a non member function of a class, that is declared as a friend usingthe keyword "friend" inside the class. By declaring a function as a friend, all the accesspermissions are given to the function.A friend function is used for accessing the non-public members of a class.A class can allow non-member functions and other classes to access its ownprivate data, by making them friends. Thus, a friend function is an ordinaryfunction or a member of another class.Need for Friend Function:As discussed in the earlier sections on access specifiers, when a datais declared as private inside a class, then it is not accessible from outsidethe class. A function that is not a member or an external class will notbe able to access the private data. A programmer may have a situation wherehe or she would need to access private data from non-member functions andexternal classes. For handling such cases, the concept of Friend functionsis a useful tool.How to define and use Friend Function in C++:The friend function is written as any other normal function, exceptthe function declaration of these functions is preceded with the keywordfriend. The friend function must have the class to which it is declared asfriend passed to it in argument.Some important points to note while using friend functions in C++:* The keyword friend is placed only in the function declaration of the friendfunction and not in the function definition..* It is possible to declare a function as friend in any number of classes..* When a class is declared as a friend, the friend class has access to theprivate data of the class that made this a friend..* A friend function, even though it is not a member function, would have therights to access the private members of the class..* It is possible to declare the friend function as either private or public..* The function can be invoked without the use of an object. The friend functionhas its argument as objects, seen in example below.properties of friend function:1. if a function to be made friend of a class than it should be declared within bodyof the class priciding with keyword friend.2.freind function never breaks the security.3.it should not be defined in name of class nor scope resolution operator is used in it'sdefination even the keyword freind is also not used while defining friend function.4.when friend function is called nither name of object nor dot operator is used. howeverit may accept the object as argument who's value it want's to access.5.it doen't matter in which section of the class we have declared a freind function.Example to understand the friend function:#includeclass exforsys{private:int a,b;public:void test(){a=100;b=200;}friend int compute(exforsys e1)//Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passedto it};int compute(exforsys e1){//Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}main(){exforsys e;e.test();cout


What is a compulsory and non compulsory?

Something is compulsory if you have to do it, if you are forced to do it. If you have a choice, it's non-compulsory.


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.


Can a friend function be friend with more than one class?

yes


What is a sentence for compulsory?

It is compulsory that you do your homework on time.


Is school compulsory in Uganda?

no it is not compulsory but there are schools there......................


What is the meaning of compulsory?

Required by rule is the main meaning of compulsory.


What is the latent function of friends?

The only function of a friend is to extend the private class interface outwith the class, essentially making the friend part of the class interface.