Recently doing a small program, a Class A (responsible for displaying processing) requires a lot of calls to Class B (responsible for data processing) of the function, I first thought of the friend concept in C + +, because these functions in class B do not want public, it just allows class A to invoke monitoring.
Online Search, there is no good simple implementation, but I think of the way I used to write, that is not the simplest implementation?
Public classB {intMa; intMB; voidFA (); voidFB (); Public classA {b b; PublicA (B bx) { This. B =BX; } voidff () {intMa =b.ma; intMB =B.MB; B.fa (); B.FB (); } } }
It is only necessary to place the definition of Class A in the implementation of Class B, at which point Class A can access all the methods and data of Class B. And it's easy to understand that a is either a part of B or a parasitic part of B. Only b exists, A has the meaning of existence.
C # to implement friend classes in C + +