answersLogoWhite

0


Best Answer

Yes you can. Although you will often hear it said that friend functions undermine encapsulation, this is errant nonsense. Here's a typical example:

#include <iostream>

using namespace std;

class Date

{

private:

int mo, da, yr;

public:

Date( int m, int d, int y ): mo(m), da(d), yr(y) {}

friend ostream& operator<<(ostream& os, const Date& dt);

};

ostream& operator<<(ostream& os, const Date& dt)

{

os << dt.mo << '/' << dt.da << '/' << dt.yr;

return os;

}

int main()

{

Date dt( 5, 6, 92 );

cout << dt;

return( 0 );

}

While it is possible to eliminate the friend function declaration simply by providing public accessors to Date::mo, Date::da and Date::yr, there is no advantage in doing so. You can still provide those accessors, of course, but since you'd need to return those members by value in order to maintain encapsulation, the operator overload would be less efficient as a result. In this case the difference is minimal but, with more complex members, the inefficiency can quickly add up.

Remember that friend functions merely extend the class interface. While it is true that you should not grant friend access to code that you have no control over (and thus undermine any concept of encapsulation), operator overloads such as the insertion operator overload shown above is under your complete control. Ultimately, if it were possible to declare the function as an actual member of the class then you would rightly do so. But that's precisely what you do when you declare friendship. And in this case, that friendship is perfectly legitimate because the alternative, non-friend function, would be less efficient.

User Avatar

Wiki User

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

Wiki User

12y ago

While overloading operators using member function it takes only one arguments(other pass implicitly)...

but in case of friend fuction two parameters are required..

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

When the operator cannot be implemented as a member function of the class, and the operator requires access to some or all of the private members of the class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you use friend functions to overload operators?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How is the precedence of operators in an expression overridden in most languages?

Precedence of operators in an expression overridden by the use of parentheses


Does C allows you to use equality operators to compare strings?

Yes, it does.


What are the application of this pointer can you use this pointer in friend function justify?

The this pointer can only be used within nonstatic member functions. Friend functions are not members so they have no access to a this pointer. However, you can pass a specific instance of a class to a function via a class reference argument. To understand how friendship works, first understand that a nonstatic member function has the following properties: 1. It has private access to the class. 2. It is scoped to the class. 3. It must be invoked upon an object of the class (has a this pointer). Static member functions have the first two properties while friend functions only have the first property.


What is containership in oops concept?

In container class we can only access the public part of base class. For accessing of private and protected part of base class we use friend functions.


What is built-in function in c?

There are no built-in functions in C++. The definition of a built-in function is a function that does not need to be declared before it is used, but every function in C++ is user-defined and must be declared before it can be used. This includes functions provided by the C++ standard library which we declare by including the appropriate headers.Some articles mistakenly describe keywords such as while, switch and if as being built-in functions, however these are statements, not functions.C++ does provide several built-in operators, some of which look and behave very much like functions. For example, the built-in sizeof() operator can be used without any declaration and looks very much like a function, but its argument is not a value, it is a type name, and we cannot (easily) define a function that accepts a type name as an argument.The built-in typeid() operator also takes a type name argument, but it is often mistakenly regarded as being a user-defined function because we must include the C++ standard library header in order to use it. But that header is only required because the return value is a std::typeinfo object which is not a built-in data type.Unlike sizeof() and typeid(), the default global new and delete operators can be overridden with user-defined function operators, thus these are also mistakenly regarded as being built-in functions. However, only the overrides are functions because that's the only way to define an operator overload; the default global operators are built-in operators, not built-in functions.The semantic difference between a built-in operator and a built-in function may seem insignificant, however a real built-in function would be no different to a user-defined function other than the fact that it need not be declared before using it. But a user-defined function also has identity (a memory address) and we can pass that identity to other functions using a function pointer argument. But we cannot pass a built-in operator to a user-defined function because it has no identity, thus it cannot be regarded as being a built-in function.

Related questions

What are special formulas that do not use operators to calculate a result?

functions


How to overload the new and delete operators to provide custom dynamic allocation of memory?

You don't. Remember that C++ is a superset of the C language. You can still use the old malloc/free functions to perform your own memory allocation/deletion.


Why should you have to use comparison operator overloading in pairs in c?

You cannot overload operators in C. This is a C++ thing only.


What are the arithmetic instructions that calculate in the spreadsheet called?

They usually are called operators. You can use addition, subtraction, multiplication, division, and exponents as operators. You can combine operators with functions.


What is a function in ICT?

Functions are pre-written formulas. Functions differ from regular formulas in that you supply the value but not the operators,such as (+,-,*,/). You can use the SUM function to add.


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


Is membership required to use Asian Friend Finder?

At this moment Asia Friend Finder is one of the most popular english-based dating operators in China. With such a glowing recommendation, one can assume clients have been satisfied with the service. To use the friend find you will need to become a member.


What is the difference between overuse and overload?

the differecne is that overload means to much overuse means that you use it to much.


Do broadcast and cable television operators use long-haul applications?

Broadcast and cable television operators use long-haul applications


How is the precedence of operators in an expression overridden in most languages?

Precedence of operators in an expression overridden by the use of parentheses


How do you function the cellular phone?

Every model of mobile phone has its own functions, invoked with different keying. Get copy of the Operators Guide/ Manual to learn how to use it. The manufacturer's website has online copies available.


List two reasons why null values might be introducedin database?

1. special requirements for its use in SQL joins. 2. special handling required by aggregate functions and SQL grouping operators