名称表示相同实体 [英] Names denoted the same entity

查看:49
本文介绍了名称表示相同实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明性区域的以下定义:

The following definition of declarative region:


在程序文本的某些部分引入了每个名称,称为
声明性区域,这是该程序在其中
有效的最大部分,也就是说,该名称可以用作
不合格名称来引用相同的名称

Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity.

我们在下面的规范中有示例:

We have example into the spec below:

int j = 24;
int main() {
    int i = j, j;
    j = 42;
}




标识符两次被声明为名称(并使用两次)。第一个j的
声明性区域包括整个示例。第一个j的
潜在范围在该j之后立即开始,
扩展到程序结尾,但其(实际)范围不包括,和}之间的
文本。 j的第二个
声明的声明区域(紧接分号前的j)包括{和}之间的所有
文本,但其潜在范围不包括i的
声明。 j的第二个声明的范围与其潜在范围相同

the identifier j is declared twice as a name (and used twice). The declarative region of the first j includes the entire example. The potential scope of the first j begins immediately after that j and extends to the end of the program, but its (actual) scope excludes the text between the , and the }. The declarative region of the second declaration of j (the j immediately before the semicolon) includes all the text between { and }, but its potential scope excludes the declaration of i. The scope of the second declaration of j is the same as its potential scope.

目前尚不清楚如何确定声明性区域为任意名称。至少我无法在标准中找到它。

It's unclear how to determine declarative region for arbitrary name. At least I can't find this into the standard.

推荐答案

在文件范围内声明的变量的潜在范围(即,而不是在名称空间,类或函数内部)是从声明变量的那一点直到文件的结尾。在函数内部声明的变量的潜在范围是从声明变量的位置开始直到声明变量所在的括号为止。

The potential scope of a variable declared at the file scope (i.e., not inside a namespace, class, or function) is from the point at which the variable is declared until the end of file. The potential scope of a variable declared inside a function is from the point at which the variable is declared until the close brace inside of which the variable was declared.

实际范围如果在某个内部范围内声明了同名的新变量,则变量的变量可能小于潜在范围。这称为 shadowing

The actual scope of a variable can be smaller than the potential scope if a new variable of the same name is declared at some inner scope. This is called shadowing.

// The following introduces the file scope variable j.
// The potential scope for this variable is from here to the end of file.
int j = 42; 

// The following introduces the file scope variable k.
int k = 0;

// Note the parameter 'j'. This shadows the file scope 'j'.
void foo (int j) 
{
    std::cout << j << '\n'; // Prints the value of the passed argument.
    std::cout << k << '\n'; // Prints the value of the file scope k.
}
// The parameter j is out of scope. j once again refers to the file scope j.


void bar ()
{
    std::cout << j << '\n'; // Prints the value of the file scope j.
    std::cout << k << '\n'; // Prints the value of the file scope k.

    // Declare k at function variable, shadowing the file scope k.
    int k = 1; 
    std::cout << k << '\n'; // Prints the value of the function scope k.

    // This new k in the following for loop shadows the function scope k.
    for (int k = 0; k < j; ++k) { 
        std::cout << k << '\n'; // Prints the value of the loop scope k.
    }
    // Loop scope k is now out of scope. k now refers to the function scope k.

    std::cout << k << '\n'; // Prints the function scope k.
}
// Function scope k is out of scope. k now refers to the file scope k.

这篇关于名称表示相同实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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