Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a member function a friend

What happens when you make a member function of a class a friend of itself!?

The code below compiles and runs. Without the friend declaration a 'too many arguments to operator' is generated (and rightly so). I realise that doing this doesn't make any sense but can anyone tell me what is happening here? Does friend force the compiler to omit the default this parameter in some way?

class Test
{
public:
  friend bool operator<(Test& lhs, Test& rhs)
  {
     return true;
  }
};

int main( int c, char** argv)
{
  Test test1;
  Test test2;

  return test1 < test2;
}
like image 485
radman Avatar asked Dec 06 '10 04:12

radman


People also ask

Can member functions be friends?

A friend function is a function that is specified outside a class but has the ability to access the class members' protected and private data. A friend can be a member's function, function template, or function, or a class or class template, in which case the entire class and all of its members are friends.

What is the function of a friend?

Friends can help you celebrate good times and provide support during bad times. Friends prevent isolation and loneliness and give you a chance to offer needed companionship, too. Friends can also: Increase your sense of belonging and purpose.

What is member function Explain friend function with example?

A friend function is a special function in C++ which in-spite of not being member function of a class has privilege to access private and protected data of a class. A friend function is a non member function or ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class.


2 Answers

The difference is that a friend is not a member even if the entire definition appears inside the class; rather, the function is placed in the surrounding namespace. So, there is no this pointer. While a member operator< operates implicitly on this and the explicit right-hand-side argument, a friend needs both left- and right-hand side argument provided explicitly as function parameters - hence the extra parameter. Your friend version is equivalent to putting the function after the class, except that it has access to the private and protected members and bases and is implicitly inline (though that doesn't mean the compiler has to inline it - it's only a hint, but it's important with respect to the One Definition Rule in that your friend function can be included from many translation units and link without issues).

like image 58
Tony Delroy Avatar answered Oct 23 '22 18:10

Tony Delroy


What happens when you make a member function of a class a friend of itself!?

That doesn't make any sense. How can a member function of a class be a friend of the same class?

You have overloaded operator < as a friend function (not as a member function). Providing the definition(body) of a friend function inside the class is legal. However it is illegal to use this inside its definition

friend bool operator<(Test& lhs, Test& rhs)
{
     *this ; //error
     return true;
}
like image 30
Prasoon Saurav Avatar answered Oct 23 '22 19:10

Prasoon Saurav