#ifndef _A_H_ #define _A_H_ #include <iostream> using namespace std; class A { private: int a; friend class B;//private public protect 权限对友元无影响 public: }; #endif #include "A.h" #ifndef _B_H_ #define _B_H_ #include <iostream> using namespace std; class B { public: void show(); }; #endif #include "B.h" #include "A.h" void B::show() { A a1; a1.a = 1; cout << "friend B::show() a1.a = " << a1.a << endl; } #ifndef _C_H_ #define _C_H_ #include "A.h" #include "B.h" class C { public: void showc(); }; #endif void C::showc() { //A A1; //A1.a = 3;非友元不能访问 cout << "C::showc()" << endl; } #include "A.h" #include "B.h" #include "C.h" int main() { B B1; B1.show(); return 0; }