Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Friend function/class, FriendOfX for some arbitrary class X is an implementation

ID: 658903 • Letter: F

Question

Friend function/class, FriendOfX for some arbitrary class X is an implementation detail of class X. Why in C++ we have to declare them in the header file? Are there any compelling reasons to make this contract (between X and FriendOfX) explicit?

I understand there could be valid reasons to make this contract explicit but in some cases I would not want my client using interface class X to know about FriendOfX since FriendOfX happens to handle certain collaboration aspects of interface class X which are implementation specific

Explanation / Answer

It's indeed an implementation details of the class, just like the whole private part is. And it is tied with the class definition for the same reason as the private part is: it is an essential part of the class which has to stay under control of the class author. If someone else could add friends, the whole public/protected/private division would not be so effective any more. (Remember the division in header and implementation files is arbitrary and nothing prevent you to split a class implementation into several files).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote