As mentioned in the previous blog post, you can record the access permission here when you think that youyuan can change the access permission.
You can use friend to apply for membership requests. Other classes cannot access private members of this type. However, you can access this type after applying for membership.
Generally, youyuan has three forms:
1) youyuan class
2) youyuan function 3) youyuan member function
1 youyuan class
Class A; // The first step declares the membership class B // The second step defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age ): age (_ age) {} friend a; // declare the friend relationship private: int age;}; Class A // step 3 defines the friend class {public: void print (const B & B) {cout <"B. age = "<B. age <Endl ;}};
2Youyuan Function
Class B // The first step defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age): Age (_ age) {} friend void print (const B & B); // declare the friend relationship private: int age;}; // define the void print (const B & B) function in step 2) {cout <"B. age = "<B. age <Endl ;}
3. member functions (the most troublesome one)
Class B; // The first step declares that you want to apply for a membership class (because it is used in the membership class) Class A // The second part can define the membership class (except for the membership function in the class, because it is used to apply for membership in the friend class, and the class currently applying for membership is not defined) {public: A () {cout <"Run constructor" <Endl ;} void test () {cout <"Run test" <Endl;} void print (const B & B); // declare the member function in the class, just declare }; class B // step 3 defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age): Age (_ age) {} friend void a: Print (const B & B); // declare the friend relationship private: int age ;}; // The fourth part defines the member function void :: print (const B & B) {cout <"B. age = "<B. age <Endl ;}