cpp nested class

#include<iostream>
  
using namespace std;
  
 /* start of Enclosing class declaration */  
class Enclosing {      
   private:   
       int x;
     
   /* start of Nested class declaration */  
   class Nested {
      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
}; // declaration Enclosing class ends here
  
int main()
{     
  
}

#include<iostream>
  
using namespace std;
  
 /* start of Enclosing class declaration */  
class Enclosing {      
        
   int x;
     
   /* start of Nested class declaration */  
   class Nested {
      int y;   
   }; // declaration Nested class ends here
  
   void EnclosingFun(Nested *n) {
        cout<<n->y;  // Compiler Error: y is private in Nested
   }      
}; // declaration Enclosing class ends here
  
int main()
{ 
     
}

ref

Nested Classes in C++ - GeeksforGeeks
https://www.geeksforgeeks.org/nested-classes-in-c/

posted on 2023-03-10 01:02  yusisc  阅读(10)  评论(0编辑  收藏  举报

导航