How to write the actual code from a nested class outside the main class

淺唱寂寞╮ 提交于 2019-12-23 10:39:57

问题


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...


回答1:


class A {
    class B;
};

class A::B {
    // ...
};



回答2:


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() ;
}


来源:https://stackoverflow.com/questions/8006830/how-to-write-the-actual-code-from-a-nested-class-outside-the-main-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!