2
class Train{ public: char direction; int loading_time, crossing_time; ... friend std::ostream& operator<<(std::ostream& os, const Train& t){ os << t.direction << '/' << t.loading_time << '/' << t.crossing_time; return os; } }; 

Why is "friend" needed in this case? All attributes are public. Should I just use a struct instead?

6
  • 1
    friend is needed because the function is (inappropriately) defined inside the class. Without friend it would be a member function, with a different signature. Commented Feb 19, 2022 at 22:50
  • 2
    When defined this way, as an inline friend function, it is a hidden friend which is only accessible through ADL. (Some teams or companies actively avoid ADL, and even possibly take steps to neutralize ADL from being utilized by the C++ compiler. I'm okay with ADL, but something to be aware of.) Commented Feb 19, 2022 at 22:58
  • "Why is "friend" needed in this case?" friend is not needed in this case. friend grants access to a class's private members. This function does not need such access. Commented Feb 19, 2022 at 23:06
  • @DrewDormann without "friend" it doesn't compile Commented Feb 19, 2022 at 23:10
  • @ChrisKouts please see it compiling successfully in the link in my last comment. Making a function that doesn't need private access a friend serves no purpose and violates encapsulation. It's not needed and I don't recommend it. Commented Feb 19, 2022 at 23:14

1 Answer 1

8

The friend is needed in order to make the function global. If you omit it, the function would be considered member function, in which case it should get only one parameter, and the caller should be of type Train (the class type in which we are declared), which doesn't fit our needs.

You can use in your case a global function instead:

class Train{ public: char direction; int loading_time, crossing_time; ... }; std::ostream& operator<<(std::ostream& os, const Train& t){ os << t.direction << '/' << t.loading_time << '/' << t.crossing_time; return os; } 

However, it is very common to use a friend function for this use case (instead of a non-friend global function), as it is declared inside the class, which is an important side bonus that you get.

Sign up to request clarification or add additional context in comments.

4 Comments

Not only “can use”, but “should use”. +1.
I think the point of making it a friend is to be able to access the data members. normally they are private
@Tootsie — all the members of Train are public.
It is also common to use a (hidden) friend for this case to reduce overload sets, with potential benefits to compile time and to diagnostics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.