Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

friend in operator == or << when should i use it?

Tags:

I feel I have a bit of a hole in my understanding of the friend keyword.

I have a class, presentation. I use it in my code for two variables, present1 and present2, which I compare with ==:

if(present1==present2)

Here's how I defined the operator == (in class presentation):

bool operator==(const presentation& p) const;

However, I was told that using friend and defining it outside of the class is better:

friend bool operator==(presentation&, presentation&);

Why? What's the difference between the two?

like image 818
Nadav Avatar asked Oct 03 '10 14:10

Nadav


People also ask

Should operator == be a friend function?

If a class is going to be on the right-hand side of the expression—and does not provide an implicit conversion to a type that can be compared with the left-hand side—you need to implement operator== as a separate function or as a friend of the class.

Why must << be overloaded as a friend function?

Why must << be overloaded as a friend function? It is not necessary. It is usually done because this function often accesses the private and protected members of the object it displays. overloaded operator<< or operator>> function.

Should I use friend in C++?

1) Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming. 2) Friendship is not mutual.

Where do you use friend function in C++?

friend Function in C++ A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.


1 Answers

Your solution works, but it's less powerful than the friend approach.

When a class declares a function or another class as friend it means that friend function or class have access to the declaring class' privates and protected members. It's as if the declared entity was a member of the declaring class.

If you define operator==() as a member function then just like with the friend case the member function has full access to the class' members. But because it is a member function it specifies a single parameter, as the first parameter is implied to be this: an object of type presentation (or a descendent thereof). If, however, you define the function as a non-member then you can specify both parameters, and this will give you the flexibility of comparing any two types that can cast into a presentation using that same function.

For example:

class presentation {
    friend bool operator==(const presentation&, const presentation&);
    // ...
};

class Foo : public presentation { /* ... */ };
class Bar : public presentation { /* ... */ };

bool operator==(const presentation& p1, const presentation& p2)
{
    // ...
}

bool func(const Foo& f, const Bar& b, const presentation& p)
{
    return f == b || f == p );
}

Lastly, this begs the question "why the friend declaration?". If the operator==() function does not need access to private members of presentation then indeed the best solution is to make it a non-member, non-friend function. In other words, don't give a function access privileges which is doesn't need.

like image 64
wilhelmtell Avatar answered Oct 21 '22 12:10

wilhelmtell