#include<iostream> using namespace std; /* start of Enclosing class declaration */ class Enclosing { private: int x; /* start of Nested class declaration */ class Nested { public: int y; void NestedFun(Enclosing *e) { cout<<e->x; // works fine: nested class can access // private members of Enclosing class } }; // declaration Nested class ends here public: int access_nested(){ Nested nested; cout<<nested.y<<endl; return 0; } }; // declaration Enclosing class ends here int main() { Enclosing enclosing_instance; enclosing_instance.access_nested(); }
本文探讨了C++中嵌套类的使用及其如何访问外部类的私有成员。通过一个具体示例,展示了嵌套类如何在内部访问其外围类的私有变量和方法。
416

被折叠的 条评论
为什么被折叠?



