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

前端 未结 30 2043
孤街浪徒
孤街浪徒 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 10:45

    At work we use friends for testing code, extensively. It means we can provide proper encapsulation and information hiding for the main application code. But also we can have separate test code that uses friends to inspect internal state and data for testing.

    Suffice to say I wouldn't use the friend keyword as an essential component of your design.

提交回复
热议问题