Introduction to the use of C ++ Zhongyou Functions
1. youyuan Function
class A{ friend void MyFriend(class &A);private: int m_a;}void MyFriend(class &A){ std::cout << A.m_a << std::endl;}
2. youyuan class
class A{public: A(); friend class B;private: int m_a;}class B{public: B(); void UseA(A &a);}void B::UseA(A &a){ std::cout<< a.m_a << std::endl;}
3. Membership functions
// Pay attention to the Declaration Order to ensure that class A is visible; // A is used for pre-declaration, because class B {public: B () must be used in B (); void UseA (A & a);} class A {public: A (); friend void B: UsaA (A & a); private: int m_a;} void B :: useA (A & a) {std: cout <. m_a <std: endl ;}
4. Mutual friend functions (one function is a friend of two classes)
class B;class A{public: A(); friend void UseAB(A &a,B&b);private: int m_a;}class B{public: B(); friend void UsaAB(A &a,B &b);private: int m_a;}void UseAB(A &a,B &b){ std::cout << a.m_a + b.m_a << std::endl;}
Youyuan is an extension of the class interface, because only in the class can you specify who is youyuan, so, does not violate the OOP idea!