在另一个函数中定义一个函数在语法上是否有效? [英] is it syntactically valid to define a function with in another function?

查看:125
本文介绍了在另一个函数中定义一个函数在语法上是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个函数中定义一个函数在语法上是否有效?

is it syntactically valid to define a function with in another function?

推荐答案

在C语言中,您不能这样做. 在C ++中,您可以间接地,例如
In C, you cannot do that.
In C++ you can indirectly, e.g.
int f()
{
   struct S {
      int g() { return 1; }
   } s;
   return s.g();
}

您可以声明一个函数中的一个函数(以便可以使用),但是它的定义必须放在其他位置,例如

You may declare a function within a function (so that it can be used), but it''s definition must be placed elsewhere, e.g.

int f()
{
   extern int g();
   return g();
}
...
int g() { return 1; }

干杯
Andi

Cheers
Andi




在C,C ++,C#甚至VB中无效.

您甚至不能在C中使用匿名委托,因此确实没有办法.
No.

It isn''t valid in C, or C++ or C#, or even VB.

You can''t even use anonymous delegates in C, so there really, really is no way to do it.


这篇关于在另一个函数中定义一个函数在语法上是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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