是有可能用C或C ++创建在另一个函数? [英] is it possible in C or C++ to create a function inside another?

查看:148
本文介绍了是有可能用C或C ++创建在另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请告诉我,如果这是可能在C或C ++?

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(){
     ...
   }
   ...
} 

或类似的东西,如例如函数内部类?

or something similar, as e.g. a class inside a function?

感谢您的回复,

推荐答案

哇,我很惊讶没有人答应了!免费功能无法嵌套,但仿函数和类的一般都可以。

Wow, I'm surprised nobody has said yes! Free functions cannot be nested, but functors and classes in general can.

void fun_a();
//int fun_b();
...
main(){
   ...
   fun_a();
   ...
   struct { int operator()() {
     ...
   } } fun_b;

   int q = fun_b();
   ...
}

您可以给仿函数的构造,并通过对局部变量的引用将它连接到本地范围。否则,它可以访问其他地方类型和静态变量。局部类不能参数模板,虽然。

You can give the functor a constructor and pass references to local variables to connect it to the local scope. Otherwise, it can access other local types and static variables. Local classes can't be arguments to templates, though.

这篇关于是有可能用C或C ++创建在另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆