answersLogoWhite

0


Best Answer

Ideally, never. Friend functions should only be employed when a function (whether an operator overload or not) requires private access to a class, and it is not otherwise possible to provide a public interface without unduly undermining the class encapsulation. However, as programmer, it is your responsibility to ensure all friend functions adhere to the same class rules (which you yourself define) as do the members of your class, even though friends are not regarded as being members of the class. Ultimately, if you have no control over the friend function implementation, then you must not allow that function to be a friend of your class, as this will seriously undermine the encapsulation.

User Avatar

Wiki User

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

Wiki User

11y ago

Whenever the operator function requires privileged access to the private class members.

This answer is:
User Avatar

Add your answer:

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

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 are the friend function implications on information hiding?

Other than that the friend has privileged access to the private members of the class in which it is declared a friend, there are no implications. However, there has to a be a reason for the friend to require that access.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


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 friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.

Related questions

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.


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


When a function needs to operate on private data in two objects of different classes it can be declared a friend of either class true or false?

False. it must be declared a friend of both classes.


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.


What is operator function?

An operator function implements a particular operator symbol. The database server provides special SQL-invoked functions, called operator functions, that implement operators. An operator function processes one to three arguments and returns a value. When an SQL statement contains an operator, the database server automatically invokes the associated operator function. The association between an operator and an operator function is called operator binding. You can overload an operator function to provide the operator for a UDT. The SQL user can then use the operator with the UDT as well as with the built-in data types. When an SQL statement contains an operator, the database server automatically invokes the associated operator function.


What are the friend function implications on information hiding?

Other than that the friend has privileged access to the private members of the class in which it is declared a friend, there are no implications. However, there has to a be a reason for the friend to require that access.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


What is friend datatype in object-oriented programming?

A friend is any class, class method or function that is declared to be a friend of a class. Friends have private access to the classes that declare them friends.


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 friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.


When is a friend function compulsory?

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.


How does friend operator function differ from member operator function?

In C++, we know that private members cannot be accessed from the outside class.That is a non-member function cannot have an access to the private data of a class.However there could be a situation where we would like two classes to share a particular function.For example consider a case where two classes, manager and scientist have been defined.We would like to use a function income_ tax () to operate on the objects of both these classes.In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:Class ABC{………..Public:……..……..Friend void xyz (void); //declaration};When the function is logically coupled with the class (like your maze connectedness example)When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .