C ++我们可以在函数里面有函数吗? [英] C++ can we have functions inside functions?

查看:180
本文介绍了C ++我们可以在函数里面有函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是

int main(){
  void a(){
  //code
  }
  a();
}


推荐答案

否,C ++不支持。

No, C++ doesn't support that.

也就是说,你可以有本地类,它们可以有函数(非 static static ),所以你可以得到一些扩展,虽然它有点kludge:

That said, you can have local classes, and they can have functions (non-static or static), so you can get this to some extend, albeit it's a bit of a kludge:

int main() // it's int, dammit!
{
  struct X { // struct's as good as class
    static void a()
    {
    }
  };

  X::a();

  return 0;
}

但是,我会问这个问题。每个人都知道(好,现在你做,反正:))C ++不支持局部函数,所以他们习惯于没有它们。然而,他们不被使用,那个kludge。我会花很多时间在这段代码,以确保它只是在那里允许本地函数。不好。

However, I'd question the praxis. Everyone knows (well, now that you do, anyway :)) C++ doesn't support local functions, so they are used to not having them. They are not used, however, to that kludge. I would spend quite a while on this code to make sure it's really only there to allow local functions. Not good.

这篇关于C ++我们可以在函数里面有函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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