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?
friend
function, then you should change the title of your question to reflect that. – Rwanda