When should you use 'friend' in C++?

前端 未结 30 2188
孤街浪徒
孤街浪徒 2020-11-22 10:12

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language.

30条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 11:01

    The canonical example is to overload operator<<. Another common use is to allow a helper or admin class access to your internals.

    Here are a couple of guidelines I heard about C++ friends. The last one is particularly memorable.

    • Your friends are not your child's friends.
    • Your child's friends are not your friends.
    • Only friends can touch your private parts.

提交回复
热议问题