Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it possible to place friend function definitions inside of a class definition?

Is it not supposed for a friend function to be explicitly defined outside of a class ?
If so why can i declare a friend function inside a class definition just like any member function ?
What is this ?
Is it only OK with some operators such as < operator or is it applicable to all operators?
If it is applicable to all of them, Is there any disadvantage for doing this ?
Should it be avoided? If so why ?

class person  { public:     bool operator<(int num)     {         return  x < num ? true : false ;     }     bool operator<(person& p)     {         return  x < p.x ? true : false ;     }      friend bool operator<(int num, person &p)     {         return  p.x < num ? true : false ;     }      void setX(int num)     {         x = num;     }  private:     int x;   }; 

Update:
I am not asking for choosing non-member operator overloading or member operator overloading.
What i want to know is that :
Why we are permitted to move the definition of friend methods inside our class definition?.
Is it not violating any things? If it is not, Why would we have friends in first place?
We could simply define overloads as member functions ( I know the limitations of member functions ) But i am saying knowing this, Why isn't compiler complaining that I haven't defined friend function outside a class definition since it doesn't need to be inside of it (because of the class parameter it has) So why are we allowed to define a friend function inside a class definition?

like image 429
Hossein Avatar asked Jul 07 '13 13:07

Hossein


People also ask

Can friend function be defined inside the class?

Friend functions can be defined (given a function body) inside class declarations. These functions are inline functions. Like member inline functions, they behave as though they were defined immediately after all class members have been seen, but before the class scope is closed (at the end of the class declaration).

Why friend function is defined outside the class?

Friend functions are not called using objects of the class because they are not within the class's scope. Without the help of any object, the friend function can be invoked like a normal member function. Friend functions can use objects of the class as arguments.

When we define a function inside a class it is called?

In C++, a function contained within a class is called a Member function.

Where should we declare a function as a friend to a class?

A friend function can be declared in the private or public section of the class. It can be called like a normal function without using the object. A friend function is not in the scope of the class, of which it is a friend. A friend function is not invoked using the class object as it is not in the scope of the class.


2 Answers

Is it not supposed for a friend function to be explicitly defined outside of a class ?

Friend functions can be defined (given a function body) inside class declarations. These functions are inline functions, and like member inline functions they behave as though they were defined immediately after all class members have been seen but before the class scope is closed (the end of the class declaration). Friend functions that are defined inside class declarations are in the scope of the enclosing class. quote

Is it only OK with some operators such as < operator or is it applicable to all operators?

It is best to try to avoid friend functions since they are opposite to what you are trying to do using a private class scope and mainly "hide" the variables. If all your functions are friend functions then what is the use of having private variables?

Still, there are some common operators which are often declared as friend functions, those are operator<< and operator>>

like image 162
Alexandru Barbarosie Avatar answered Sep 17 '22 18:09

Alexandru Barbarosie


If you are creating a header-only class (which makes deployment vastly easier) then defining a friend function within the class is the only way to go since definitions can only appear in a single translation unit. The normal technique of include guards doesn't work since that only handles things like recursive inclusion.

This can be a big deal if you are trying to write standards-conformant code. For example, to implement the RandomNumberEngine named requirement from the C++ normative standard, it is necessary to provide operator<<. This has to be a friend to take a std::ostream& object as its first parameter (otherwise it will look like a normal, single parameter member function operator overload). Ordinarily the friend declaration would go in the class definition and and the function definition in a separate .cpp source file. But if you want a header-only implementation, it must be defined in the class to avoid multiple definition errors.

like image 39
David G Avatar answered Sep 20 '22 18:09

David G