简体   繁体   English

报关地点是什么意思?

[英]What is meant by locality of declaration?

I was reading a book and here, a program is given like this -我正在读一本书,这里给出了这样的程序 -

#include<fstream>
#include<string>
#include<vector>

int main()
{
    string filename; // #1

    cout << "Please enter name of file to open : ";
    cin >> filename;

    if(filename.empty())
    {
        cerr << "Something...";
    }
    ifstream inFile(filename.c_str());  // #2
    if(!inFile)
    {
        cerr<< "Somthing...";
    }
    .
    .
    .
}

And the explanation paragraph says, that, the declaration statements exhibit locality of declaration , Which is explained like this解释段落说,声明声明展示了声明的局部性,解释如下

the declaration statements occur within the locality of the first use of defined objects.声明语句出现在第一次使用已定义对象的地方。

I am very confused with that sentence and I am not able to understand what it actually means.我对那句话很困惑,无法理解它的实际含义。 I need an explanation with some example.我需要一些例子的解释。

the declaration statements occur within the locality of the first use of defined objects.声明语句出现在第一次使用已定义对象的地方。

Is another way to say don't declare a thing until you need it.另一种说法是在需要之前不要声明某物。 By doing so you bring the declaration to where the object is used and doing so makes it easier to know what that object is.通过这样做,您将声明带到使用 object 的地方,这样做可以更容易地了解 object 是什么。

Imagine you have a function that is 1000 lines long.假设您有一个 function,它有 1000 行长。 If you declare all the variables you use in the function at the start, but don't happen to use one of them until line 950, then you have to scroll back 950 lines to figure out what the type of that variable.如果您一开始就在 function 中声明了您使用的所有变量,但直到第 950 行才碰巧使用其中一个变量,那么您必须向后滚动 950 行才能弄清楚该变量的类型。 If you instead declare it on line 949, and use it on line 950, then the information is right there and you don't need to hunt it down as much.如果您改为在第 949 行声明它,并在第 950 行使用它,那么信息就在那里,您不需要那么多地寻找它。

So in your example #2 is declared right before it's used, instead of at the top like #1 is.因此,在您的示例中,#2 是在使用之前声明的,而不是像 #1 那样在顶部声明。

There are several different places in which you can declare variables in a C++ module.在 C++ 模块中有几个不同的地方可以声明变量。 For example, one can declare all variables at the beginning of that module, like in the following example:例如,可以在该模块的开头声明所有变量,如下例所示:

int MyFunc(int a, int b)
{
    int temp1, temp2, answer;
    temp1 = a + b * 3;
    temp2 = b + a * 3;
    answer = temp1 / temp2;
    return (answer % 2);
}

Alternatively, as in the code you have cited, one could declare each variable immediately before it is first used, as follows:或者,如在您引用的代码中,可以在每个变量首次使用之前立即声明每个变量,如下所示:

int MyFunc(int a, int b)
{
    int temp1 = a + b * 3;
    int temp2 = b + a * 3;
    int answer = temp1 / temp2;
    return (answer % 2);
}

Both are valid styles and each will have its supporters and opponents.两者都是有效的 styles 并且每个都有其支持者和反对者。 The latter uses declarations that are in the locality of their first use .后者使用位于其首次使用地点的声明

In these simple examples, the difference in styles is really trivial;在这些简单的例子中,styles 的差别真的很小; however, for functions that have (say) 100+ lines of code, using such 'local' declarations can enable a future reader of the code to appreciate the nature of a variable without having to 'scroll back up' to the the start of that function.但是,对于具有(比方说)100 多行代码的函数,使用此类“本地”声明可以使代码的未来读者能够理解变量的性质,而不必“向上滚动”到该变量的开头function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM