本地功能...... [英] Local Functions...

查看:70
本文介绍了本地功能......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为什么C ++在函数中限制函数的定义?

int f1(){

int f2(){

.....

}

} //不支持。


谢谢推荐给任何回复的人..


Why does C++ restrict definition of functions with in functions?
int f1() {
int f2() {
.....
}
} //This is not supported.

Thanks in advance to anybody who replies..

推荐答案

2007-08-29 11:33, de ********* @ gmail.com 写道:
On 2007-08-29 11:33, de*********@gmail.com wrote:

为什么C ++在函数中限制函数的定义?

int f1(){

int f2(){

.... 。

}

} //这不受支持。


提前感谢任何回复的人..
Why does C++ restrict definition of functions with in functions?
int f1() {
int f2() {
.....
}
} //This is not supported.

Thanks in advance to anybody who replies..



我没有详细研究过这个想法,但我不能提出任何可以通过嵌套函数实现的
完成

正常功能。事实上我发现它有点限制,因为f2()不能从f1()之外的范围调用
,这意味着你不能重用f2()
在其他一些情况下



-

Erik Wikstr?m

I have not studied the idea in detail but I can''t come up with anything
that can be accomplished with nested functions that can''t be done with
normal functions. In fact I find it a bit limiting since f2() can''t be
called from a scope outside f1(), which means that you can''t reuse f2()
in some other context.

--
Erik Wikstr?m


Erik Wikstr?m写道:
Erik Wikstr?m wrote:

2007-08-29 11:33, de ********* @ gmail.com 写道:

>为什么C ++限制函数的定义在函数中?
int f1(){
int f2(){
.....
}
} //这不受支持。 />
提前感谢任何回复的人..
>Why does C++ restrict definition of functions with in functions?
int f1() {
int f2() {
.....
}
} //This is not supported.

Thanks in advance to anybody who replies..



我没有详细研究这个想法,但我不能想出任何东西

可以通过嵌套函数来完成,这些函数不能用

正常函数完成。事实上我发现它有点限制,因为f2()不能从f1()之外的范围调用
,这意味着你不能重用f2()
在其他一些情况下


I have not studied the idea in detail but I can''t come up with anything
that can be accomplished with nested functions that can''t be done with
normal functions. In fact I find it a bit limiting since f2() can''t be
called from a scope outside f1(), which means that you can''t reuse f2()
in some other context.



你可以将f2提供给某些std :: algorithm


void f2(double * ptr){

.....

}

std: :for_each(ptr,ptr + N,f2);

}


这里f2的定义是(可能)单个地方的本地/>
使用它。


matthias


you could feed f2 into some std::algorithm

int f1() {
void f2(double* ptr) {
.....
}
std::for_each(ptr, ptr+N, f2);
}

here the definition of f2 is local to the (probably) single place where
it is used.

matthias


< de ********* @ gmail.com写信息

新闻:11 ********************** @ m37g2000prh.googlegr oups.com ...
<de*********@gmail.comwrote in message
news:11**********************@m37g2000prh.googlegr oups.com...

>

为什么C ++会限制函数中函数的定义?
>
Why does C++ restrict definition of functions with in functions?



因为C ++有类。真的,为什么你需要这样的功能级

封装,当你能做一个更逻辑和定义的类

封装时,我想知道吗?


-

Abdo Haji-Ali

程序员

In | Framez

Because C++ has classes. Really, why would you need such function-level
encapsulation when you can do a more logical and defined class
encapsulation, I wonder?

--
Abdo Haji-Ali
Programmer
In|Framez


这篇关于本地功能......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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