is it possible in C or C++ to create a function inside another?

后端 未结 15 1997
迷失自我
迷失自我 2020-12-15 09:18

Could someone please tell me if this is possible in C or C++?

void fun_a();
//int fun_b();
...
main(){
   ...
   fun_a();
   ...
   int fun_b(){
     ...
            


        
15条回答
  •  情歌与酒
    2020-12-15 09:51

    you can't create a function inside another function in C++.

    You can however create a local class functor:

    int foo()
    {
       class bar
       {
       public:
          int operator()()
          {
             return 42;
          }
       };
       bar b;
       return b();
    }
    

    in C++0x you can create a lambda expression:

    int foo()
    {
       auto bar = []()->int{return 42;};
       return bar();
    }
    

提交回复
热议问题