简体   繁体   English

C++ 中的局部/静态变量作用域

[英]Local/static variable scope in C++

If I write something like this:如果我写这样的东西:

#include <iostream>

int main()
{
    using namespace std;

    {int n;n=5;} cout<<n;
    system("pause");
    return 0;
}

The compiler tells me that n is undeclared.编译器告诉我 n 未声明。 Then I tried making it static, but again, the compiler tells me that it is undeclared.然后我尝试将其设为静态,但编译器再次告诉我它未声明。 Doesn't a variable declated static have program scope?声明为静态的变量没有程序范围吗? If not, how do I use n in this program?如果没有,我如何在这个程序中使用 n?

You're confusing scope with lifetime.您将范围与生命周期混淆了。 Static variables have a lifetime equal to the program's lifetime, but they still follow scoping rules based on where they are declared.静态变量的生命周期等于程序的生命周期,但它们仍然遵循基于声明位置的作用域规则。

The scope of n is just between the brackets: n 的范围就在括号之间:

{int n;n=5;}

so outside of the block, you have no n variable.所以在块之外,你没有 n 变量。

Making it static just makes it's value retain even after you exit the block so that the next time you enter that block again, you can retrieve it's value from the last time you executed that block, but still it's scope is still within the brackets.将其设为静态只会使其在退出块后的值保持不变,以便下次再次进入该块时,您可以从上次执行该块时检索它的值,但它的范围仍然在括号内。

A variable declared static in the global scope has its scope limited to the translation unit.在全局范围内声明为静态的变量的范围仅限于翻译单元。 A variable declared static within a function has its lifetime set to be the same as the program's, but in this case does not affect its scope.在函数中声明为 static 的变量的生命周期设置为与程序的生命周期相同,但在这种情况下不会影响其范围。 You will have to put cout in the same scope as n was declared in order to use it.您必须将cout放在与n声明相同的范围内才能使用它。

Here the compiler gives error n is undeclared because here " {int n;n=5;} " it is declared in the braces .这里编译器给出错误 n is undeclared 因为这里“ {int n;n=5;}它是在大括号中声明的 And braces tells us about the scope of the variable.大括号告诉我们变量的作用域。 When ever the scope ends the variable is deleted from the memory .当作用域结束时,变量就会从内存中删除

And for Static and local.而对于静态和本地。

Static : The variable is same like global variable but its value remains constant throughout the application.静态:该变量与全局变量相同,但其值在整个应用程序中保持不变。 And the static variable cannot be used on the other page using extern .并且不能在使用extern的其他页面上使用静态变量。

Local : The local variables are stored in the stack and they are deleted when they get out of scope .本地:本地变量存储在堆栈中,超出范围时将被删除。

how do I use n in this program?我如何在这个程序中使用 n?

using namespace std;
int main()
{
     int n;      // declare n as int
     n=5;        // assign it a value
     cout << n;  // display it.
     system("pause");
     return 0;
}

Please don't be confuse between scope and life time of a static variable.请不要混淆静态变量的作用域和生命周期。 Scope means where can you access the variable.范围意味着您可以访问变量的位置。 Life time of variable is duration for which variable stay in memory.变量的生命周期是变量在内存中停留的时间。 In your case, Scope of x varible is within the curly braces.在您的情况下, x 变量的范围在花括号内。 Life time of x would be program scope. x 的生命周期将是程序范围。

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

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