Friend Class In C++

后端 未结 4 649
轮回少年
轮回少年 2021-01-19 19:11

here im not understanding the concept very well or i am right.... So lets take this \"friend\" class example here:

class MyClass{
friend class AnotherClass;
         


        
4条回答
  •  醉梦人生
    2021-01-19 19:32

    Consider the following use-case that I encountered recently: I refactored some code from one class into another class. This new class had to access members from the original class but I did not want to provide this via public getters to avoid other clients messing around with these. In this case, I really welcomed the C++-friendship mechanism.

    However, these use cases are very seldom (hopefully, otherwise there is probably something wrong in your SW architecture) and I try to avoid it as much as I can since it is the tightest form of coupling.

提交回复
热议问题