繁体   English   中英

Static 成员函数中的变量

[英]Static variables in member functions

有人可以解释一下成员函数中的 static 变量如何在 C++ 中工作。

给定以下 class:

class A {
   void foo() {
      static int i;
      i++;
   }
}

如果我声明A多个实例,在一个实例上调用foo()是否会在所有实例上增加 static 变量i 还是只有它被调用的那个?

我假设每个实例都有自己的i副本,但是单步执行我拥有的一些代码似乎表明并非如此。

由于class A是非模板 class 并且A::foo()是非模板 function。 程序内将只有一份static int i的副本。

A object 的任何实例都会影响相同的i ,并且i的生命周期将在整个程序中保持不变。 添加示例:

A o1, o2, o3;
o1.foo(); // i = 1
o2.foo(); // i = 2
o3.foo(); // i = 3
o1.foo(); // i = 4

不幸的是,关键字static在 C++ 中有几个不同的不相关含义

  1. 当用于数据成员时,这意味着数据在 class 中而不是在实例中分配

  2. 当用于 function 内部的数据时,这意味着数据是静态分配的,在第一次进入块时初始化并持续到程序退出。 此外,该变量仅在 function 内部可见。 局部静态的这一特殊功能通常用于实现单例的惰性构造。

  3. 当在编译单元级别(模块)使用时,这意味着该变量就像一个全局变量(即在main运行之前分配和初始化并在main退出之后销毁)但该变量在其他编译单元中将不可访问或可见

我强调了对每种用途最重要的部分。 使用 (3) 有点不鼓励使用未命名的命名空间,它还允许未导出的 class 声明。

在您的代码中, static关键字与含义数字 2 一起使用,与类或实例无关……它是function的变量,并且只有一个副本。

正如iammilind所说的那样,如果 function 是模板 function (因为在这种情况下 ZC1C425268E68385D14AB5074C17A4 本身确实可以存在许多不同的副本),则该变量可能存在多个实例。 即使在那种情况下,课程类和实例也无关紧要......请参见以下示例:

#include <stdio.h>

template<int num>
void bar()
{
    static int baz;
    printf("bar<%i>::baz = %i\n", num, baz++);
}

int main()
{
    bar<1>(); // Output will be 0
    bar<2>(); // Output will be 0
    bar<3>(); // Output will be 0
    bar<1>(); // Output will be 1
    bar<2>(); // Output will be 1
    bar<3>(); // Output will be 1
    bar<1>(); // Output will be 2
    bar<2>(); // Output will be 2
    bar<3>(); // Output will be 2
    return 0;
}

Static 变量内部函数

  • Static 变量在 function 内创建,存储在程序的 static ZCD69B4957F06CD8129DZ7BF3 上。

  • Static 变量初始化将在第一次调用 function 时完成。

  • Static 变量将保留多个 function 调用中的值

  • static 变量的生命周期是 Program

在此处输入图像描述

例子

#include <iostream>

using namespace std;

class CVariableTesting 
{
    public:
    
    void FuncWithStaticVariable();
    void FuncWithAutoVariable();

};

void CVariableTesting::FuncWithStaticVariable()
{
    static int staticVar = 0; //staticVar is initialised by 0 the first time
    cout<<"Variable Value : "<<staticVar<<endl;
    staticVar++;
}
void CVariableTesting::FuncWithAutoVariable()
{
    int autoVar = 0;
    cout<<"Variable Value : "<<autoVar<<endl;
    autoVar++;
}
    

int main()
{
    CVariableTesting objCVariableTesting;
    cout<<"Static Variable";
    objCVariableTesting.FuncWithStaticVariable();
    objCVariableTesting.FuncWithStaticVariable();
    objCVariableTesting.FuncWithStaticVariable();
    objCVariableTesting.FuncWithStaticVariable();
    objCVariableTesting.FuncWithStaticVariable();
    
    cout<<endl;
    cout<<"Auto Variable";
    objCVariableTesting.FuncWithAutoVariable();
    objCVariableTesting.FuncWithAutoVariable();
    objCVariableTesting.FuncWithAutoVariable();
    objCVariableTesting.FuncWithAutoVariable();
    objCVariableTesting.FuncWithAutoVariable();
    
    return 0;
}

Output:

Static 变量

变量值:0
变量值:1
变量值:2
变量值:3
变量值:4

自动变量

变量值:0
变量值:0
变量值:0
变量值:0
变量值:0

简化答案:

Static variables, regardless whether they are members of a (non-templated) class or a (non-templated) function, behave - technically - like a global label which scope is limited to the class or function.

暂无
暂无

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

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