如下图所示
内部类可以访问外部类的私有成员
而外部类无法访问内部类的私有成员
为了能让外部类访问内部类的私有成员,将外部类声明为内部类的友元类即可
#include <iostream> using namespace std; class Outer { public: class Inner { friend class Outer; //Outer是Inner的友元类 public: void func(const Outer &outer) const { cout << "I'm Inner." << endl; cout << "He's " << outer.name << endl; } private: const char *const name = "Inner"; }; void func(const Inner &inner) const { cout << "I'm Outer." << endl; cout << "He's " << inner.name << endl; } private: const char *const name = "Outer"; }; int main() { Outer().func(Outer::Inner()); cout << endl; Outer::Inner().func(Outer()); return 0; }