Can someone explain the point of friend classes?

dfi

Golden Member
Apr 20, 2001
1,213
0
0
As found by doing a quick search on google:

"You often need to split a class in half when the two halves will have different numbers of instances or different lifetimes. In these cases, the two halves usually need direct access to each other (the two halves used to be in the same class, so you haven't increased the amount of code that needs direct access to a data structure; you've simply reshuffled the code into two classes instead of one). The safest way to implement this is to make the two halves friends of each other.

If you use friends like just described, you'll keep private things private."

dfi
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
C++ generally doesn't try to dictate how you design things; there's a lot of things you can violate in C++!

edit: BTW, if you're learning C++, familiarize yourself with the C++ FAQ Lite. It explains all kinds of things, including this.