在语句块变量范围 [英] variable scope in statement blocks

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

问题描述

 的for(int i = 0;我小于10;我++)
{
    美孚();
}
INT I = 10; //错误,'我'已经存在----------------------------------------的for(int i = 0;我小于10;我++)
{
    美孚();
}
我= 10; //错误,'我'不存在

根据我的理解范围,第一个例子应该罚款。他们都没有被允许的事实似乎更奇怪。肯定i是无论是在范围或没有。

有什么非显而易见性有关的范围,我不明白,这意味着真正的编译器无法解决呢?或者仅仅是保姆状态compilerism的情况?


解决方案

  

根据我的理解范围,第一个例子应该罚款。


您的理解范围是罚款。这不是一个范围错误。这是不一致的使用简单名称的错误。


  

INT I = 10; //错误,'我'已经存在


那是不是说报告错误即可。该报告错误,是命名的我不能在此范围内的声明局部变量,因为它会给予不同的意义,我这已经是一个孩子的范围用于其他代表什么

该错误消息告诉你是什么错误;再次读取错误消息。它无处说,有声明之间的冲突;它说,该误差的,因为这改变了简单的名称的意思。该误差的不可以的重复声明;这是完全合法的在具有相同名称的两个不同的范围两件事情,即使这些范围窝。什么是的的法律是有一个简单的名字的意思的在嵌套的局部变量声明空间两码事的。

您会得到错误,如果不是你做了评选我是在此范围内已定义的局部变量像

  INT I = 10;
INT I = 10;


  

当然i是无论是在范围或没有。


当然 - 还等什么?无论是一个给定的我是在范围上还是不无关。例如:

  C类
{
    INT I;
    无效M()
    {
        字符串我;

完全合法的。外i是范围遍及M.是没有问题的所有声明阴影外部范围的局部我。如果你说这将是一个问题。

  C类
{
    INT I;
    无效M()
    {
        INT X = 1;
        的foreach(CHAR我...

由于现在你已经用我的意思在两个嵌套局部变量声明空间的两个不同的东西 - 一个循环变量和一个字段。这是令人困惑,而且容易出错,所以我们将其非法的。


  

有什么非显而易见性有关的范围,我不明白,这意味着真正的编译器无法解决这个问题?


我不明白的问题。显然,编译器的的能够完全分析程序; 如果编译器无法解析我每次使用的含义,然后怎么会报告错误信息?编译器是完全能够确定你使用的i意味着两个不同的东西同样的局部变量声明空间,并且错误相应报告。

for (int i = 0; i < 10; i++)
{
    Foo();
}
int i = 10; // error, 'i' already exists

----------------------------------------    

for (int i = 0; i < 10; i++)
{
    Foo();
}
i = 10; // error, 'i' doesn't exist

By my understanding of scope, the first example should be fine. The fact neither of them are allowed seems even more odd. Surely 'i' is either in scope or not.

Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this? Or is just a case of nanny-state compilerism?

解决方案

By my understanding of scope, the first example should be fine.

Your understanding of scope is fine. This is not a scoping error. It is an inconsistent use of simple name error.

int i = 10; // error, 'i' already exists

That is not the error that is reported. The error that is reported is "a local variable named i cannot be declared in this scope because it would give a different meaning to i which is already used in a child scope to denote something else"

The error message is telling you what the error is; read the error message again. It nowhere says that there is a conflict between the declarations; it says that the error is because that changes the meaning of the simple name. The error is not the redeclaration; it is perfectly legal to have two things in two different scopes that have the same name, even if those scopes nest. What is not legal is to have one simple name mean two different things in nested local variable declarations spaces.

You would get the error "a local variable named i is already defined in this scope" if instead you did something like

int i = 10;
int i = 10;

Surely 'i' is either in scope or not.

Sure -- but so what? Whether a given i is in scope or not is irrelevant. For example:

class C 
{
    int i;
    void M()
    {
        string i;

Perfectly legal. The outer i is in scope throughout M. There is no problem at all with declaring a local i that shadows the outer scope. What would be a problem is if you said

class C 
{
    int i;
    void M()
    {
        int x = i;
        foreach(char i in ...

Because now you've used i to mean two different things in two nested local variable declaration spaces -- a loop variable and a field. That's confusing and error-prone, so we make it illegal.

Is there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this?

I don't understand the question. Obviously the compiler is able to completely analyze the program; if the compiler could not resolve the meaning of each usage of i then how could it report the error message? The compiler is completely able to determine that you've used 'i' to mean two different things in the same local variable declaration space, and reports the error accordingly.

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

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