Friend scope in C++

前端 未结 5 927
萌比男神i
萌比男神i 2020-11-28 06:05

If I have three classes, A, B, C. A and B are friends (bidirectionally). Also, B and C are friends (bidirectionally). A has a pointer to B and B has a pointer to C. Why can\

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 07:00

    Because in C++ friendship is not a transitive property. Actually it should be avoided whenever possible because it introduces complexity in a system.

    Imagine that B is a mediator class and A and C are components that need to be managed, do you really think it makes sense that a button should need access to the implementation of a checkbox?

    By the way, I don't see where the 'hierarchy' of your title is in the case you ask.

提交回复
热议问题