C ++我们可以在函数里面有函数吗? [英] C++ can we have functions inside functions?
问题描述
我的意思是
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屋!