什么是“静态”意味着当声明“全局”变量在C ++? [英] What exactly does "static" mean when declaring "global" variables in C++?

查看:117
本文介绍了什么是“静态”意味着当声明“全局”变量在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以前的我的问题

究竟是什么是静态,它是如何使用的,什么是使用静态

What exactly is "static", how is it used, and what is the purpose of using "static" when dealing with C++?

感谢。

推荐答案

关键字 static 在C ++中有不同的含义,取决于上下文。

The keyword static has different meanings in C++, depending on the context.

当声明自由函数或全局变量时,这意味着该函数不能在单个翻译单元之外使用:

When declaring a free function or a global variable it means that the function is not to be available outside of this single translation unit:

// test.cpp
static int a = 1;
static void foo() {}

如果编译翻译单元的结果是链接使用包含符号 a foo 的不同翻译单元,它不会破坏单一定义规则,单元 a foo 私有符号。此用法已被未命名的命名空间淘汰。

If the result of compiling that translation unit is linked with a different translation unit containing symbols a and foo it will not break the One Definition Rule, as in this particular translation unit a and foo are private symbols. This use has been obsoleted by unnamed namespaces.

// test2.cpp
namespace {
   static int a = 1;
   static void foo() {}
}

声明局部变量在函数中,它意味着变量的生命周期将从第一次调用函数扩展到程序结束,而不仅仅是在调用期间:

When declaring a local variable within a function it means that the lifetime of the variable will extend from the first call to the function to the end of the program, and not only for the duration of the call:

int foo() {
   static int counter = 0;
   return ++counter;
}
int main() {
  for ( int i = 0; i < 10; ++i ) { 
     std::cout << foo() << std::endl;
  }
}

在上面的代码中,第一次调用 foo 时,计数器初始化一次,但变量将超过该函数,并保留跨不同函数调用的值。前面的代码将打印1 2 3 4 ... 10。如果变量未声明 static ,则输出将为1 1 1 ... 1。

In the previous code, counter is initialized once when foo is called for the first time, but the variable will outlive the function and keep the value across different function calls. The previous code will print "1 2 3 4... 10". If the variable was not declared static then the output would be "1 1 1... 1".

在类范围内, static 表示成员是类的成员,而不是特定实例的成员。这个使用等效于你的其他问题的使用:该特定成员的使用没有绑定到任何特定对象。

Within a class scope, static means that the member is a member of the class, and not of a particular instance. This use is equivalent to the use in your other question: usage of that particular member is not bound to any specific object.

struct test {
   int x;
   static int y;
};
int test::y;       // need to define it in one translation unit
int main() {
   // test::x = 5; // !error cannot access a non-static member variable
                   // without an instance
   test::y = 5;    // ok
   test t, other;
   t.x = 10;       // ok
   t.y = 15;       // ok, the standard allows calling a static member through
                   // an instance, but this is the same as test::y
}

在这种情况下,成员 x 是一个非静态成员属性,因此有一个不同的 x 。在示例程序 t.x other.x 中引用不同的整数。另一方面, y static ,因此有一个单独的实例 test :: y 。即使标准允许调用 t.y other.y ,两个使用引用相同的变量。成员方法也是如此。如果它们是静态的,它们是类级方法,并且可以在没有实例的情况下被调用,而如果它们是非静态的,则它们被应用于具体实例,并且 ab 必须使用code> a-> b 语法。

In this case, the member x is a non-static member attribute, and as such there is a different x for each instance of the class. In the sample program t.x and other.x refer to different integers. On the other hand y is static and thus there is a single instance of test::y in the program. Even if the standard allows to call t.y and other.y both uses refer to the same variable. The same goes with member methods. If they are static they are class-level methods and can be called without an instance, while if they are non-static they are applied to a concrete instance and the a.b or a->b syntax must be used.

使用 static 类似于在Java中使用相同的关键字,而其他两个在该语言中不存在。有一个使用Java中的关键字在C ++中不存在,这是使用静态类初始化器(在类级别的代码块包围 static {...} )。在Java中,代码块将在类加载时执行,并且只有一次。 C ++中静态成员变量的初始化必须在变量定义的初始化器中完成。

This use of static is similar to the use of the same keyword in Java, while the other two are not present in that language. There is one use of the keyword in Java that is not present in C++, and that is the use of static class initializers (a block of code at class level surrounded by static { ... }). In Java that block of code will be executed when the class is loaded and only once. Initialization of static member variables in C++ must be done in the initializer of the variable definition.

这篇关于什么是“静态”意味着当声明“全局”变量在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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