for循环中的变量范围 [英] variable scope in for loop

查看:117
本文介绍了for循环中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我们进行了代码审核,其参数为

是否i超出范围如图所示

以下:

for(int i = 0; i< 2004; i ++)

{

doSomething(i);

}


for(i = 0; i< 2004; i ++)

{

doSomethingElse(i);

}


根据C ++语言是否正确?

VC ++ 6.0与上面的代码一起工作正常。

谢谢和最好的问候,

文杰

解决方案

Wenjie在新闻中写道:d2 ************************** @ posting.google.c om:

我们使用
的参数进行了代码审查,无论是i还是i。超出范围,如下图所示:
for(int i = 0; i< 2004; i ++)
{
doSomething(i);
}

for(i = 0; i< 2004; i ++)
{
doSomethingElse(i);
}

是否正确根据C ++语言?


Nope,

VC ++ 6.0适用于上面的代码。




它的a这个编译器众所周知的缺陷,以至于MSVC 7.1

在默认情况下也表现得这样(它有2个选项可以删除

缺陷/ Za和/ Zc:forScope)。

Rob。

-
http://www.victim-prime.dsl.pipex.com/


你好,

go_S@yahoo.com (文杰)在留言中写道:< d2**************************@posting.google。 com> ...


我们进行了代码审查,其中包含/ i的参数。超出范围,如下图所示:
for(int i = 0; i< 2004; i ++)
{
doSomething(i);
}

for(i = 0; i< 2004; i ++)
{
doSomethingElse(i);
}

是否正确根据C ++语言?
VC ++ 6.0可以正常使用上面的代码。




不,这不正确。 i的范围在第一个

循环结束时结束。


如果你需要VC ++ 6的可移植性,一个很好的解决方法恕我直言br />
人工块内的循环:


{

for(int i = 0; i< 2004; i ++){

doSomething(i);

}

}


{

for(int i = 0; i< 2004; i ++){

doSomethingElse(i);

}

}


-

Wagner


go **** @ yahoo.com (Wenjie)写道:

我们用/ i的论证进行了代码审查。超出范围,如下图所示:
for(int i = 0; i< 2004; i ++)
{
doSomething(i);
}

for(i = 0; i< 2004; i ++)
{
doSomethingElse(i);
}

是否正确根据C ++语言?


不,它不是。

VC ++ 6.0可以正常使用上面的代码。




嗯,这个编译器因其标准一致性而闻名,是吗?在EDG前端尝试

一个基础,或者更好的是,验证标准中的内容。

-

< mailto: di***********@yahoo.com> < http://www.dietmar-kuehl.de/>

Phaidros eaSE - 简易软件工程:< http://www.phaidros.com/>


Hello,
We had a code review with the argument of
whether "i" is out of scope as illustrated
below:
for (int i=0; i<2004; i++)
{
doSomething(i);
}

for (i=0; i<2004; i++)
{
doSomethingElse(i);
}

Is it correct according to C++ language?
VC++6.0 works fine with above code.
Thanks and best regards,
Wenjie

解决方案

Wenjie wrote in news:d2**************************@posting.google.c om:

We had a code review with the argument of
whether "i" is out of scope as illustrated
below:
for (int i=0; i<2004; i++)
{
doSomething(i);
}

for (i=0; i<2004; i++)
{
doSomethingElse(i);
}

Is it correct according to C++ language?
Nope,
VC++6.0 works fine with above code.



Its a well known defect with this compiler, so much so that MSVC 7.1
also behaves this way by default (it has 2 option''s that can remove
the defect /Za and /Zc:forScope).
Rob.
--
http://www.victim-prime.dsl.pipex.com/


Hello,

go****@yahoo.com (Wenjie) wrote in message news:<d2**************************@posting.google. com>...


We had a code review with the argument of
whether "i" is out of scope as illustrated
below:
for (int i=0; i<2004; i++)
{
doSomething(i);
}

for (i=0; i<2004; i++)
{
doSomethingElse(i);
}

Is it correct according to C++ language?
VC++6.0 works fine with above code.



No, it''s not correct. The scope of i ends at the end of the first
loop.

If you need VC++6 portability, a good workaround IMHO is to enclosure
the loops inside artificial blocks:

{
for (int i=0; i<2004; i++) {
doSomething(i);
}
}

{
for (int i=0; i<2004; i++) {
doSomethingElse(i);
}
}

--
Wagner


go****@yahoo.com (Wenjie) wrote:

We had a code review with the argument of
whether "i" is out of scope as illustrated
below:
for (int i=0; i<2004; i++)
{
doSomething(i);
}

for (i=0; i<2004; i++)
{
doSomethingElse(i);
}

Is it correct according to C++ language?
No, it is not.
VC++6.0 works fine with above code.



Well, this compiler is not known for its standard conformance, is it? Try
one base on the EDG front-end or, better yet, verify things in the standard.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>


这篇关于for循环中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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