How to write the actual code from a nested class o

2019-06-23 08:16发布

I would like to keep the code readable by writing the actual code of a nested class outside the main class, Is it possible, and how ?

class AA{
   //random code

   class BB : public CC <double> {
      // very long code
   };

   // random code
};

I would like to write something like :

class AA{
  //random code
  //<declaration of class BB>
  // random code
};

class BB : public CC <double>{
  // very long code
};

and the BB class should only be accessible within the AA class...

标签: c++ class nested
2条回答
Deceive 欺骗
2楼-- · 2019-06-23 08:47

Is this what you want?

#include <iostream>
using namespace std ;
class AA{
   class BB{
      friend class AA ;
      void VeryLongFunction() ;
   };
public:
    void f(){
      BB bb ;
      bb.VeryLongFunction() ;
    }
};

void AA::BB::VeryLongFunction(){
  cout << "I am a very long function" << endl ;
}

int main(){
  AA aa ;
  aa.f() ;
}
查看更多
聊天终结者
3楼-- · 2019-06-23 08:49
class A {
    class B;
};

class A::B {
    // ...
};
查看更多
登录 后发表回答