实例方法中的静态变量

发布于 2024-08-19 06:52:32 字数 322 浏览 4 评论 0原文

假设我有这个程序:(

class Foo {
 public:
    unsigned int bar () {
        static unsigned int counter = 0;
        return counter++;
    }
};

int main ()
{
    Foo a;
    Foo b;
}

当然这个例子没有意义,因为我显然将“counter”声明为私有属性,但这只是为了说明问题)。

我想知道 C++ 在这种情况下的行为如何:bar() 方法中的变量“counter”对于每个实例都相同吗?

Let's say I have this program:

class Foo {
 public:
    unsigned int bar () {
        static unsigned int counter = 0;
        return counter++;
    }
};

int main ()
{
    Foo a;
    Foo b;
}

(Of course this example makes no sense since I'd obviously declare "counter" as a private attribute, but it's just to illustrate the problem).

I'd like to know how C++ behaves in this kind of situation: will the variable "counter" in the bar() method be the same for every instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江心雾 2024-08-26 06:52:32

是的,counter 将在可执行文件中 Foo 类型的所有对象实例之间共享。只要您处于单线程环境中,它就会像共享计数器一样按预期工作。

在多线程环境中,您将需要调试有趣的竞争条件:)。

Yes, counter will be shared across all instances of objects of type Foo in your executable. As long as you're in a singlethreaded environment, it'll work as expected as a shared counter.

In a multithreaded environment, you'll have interesting race conditions to debug :).

幽梦紫曦~ 2024-08-26 06:52:32

通过“每个实例都相同”,您的意思是每个类实例之间都会共享该变量的一个实例,那么是的,这是正确的。该类的所有实例都将使用相同的变量实例。

但请记住,对于类变量,在许多情况下您必须考虑多线程之类的事情,这是一个完全不同的主题。

By "be the same for every instance" you mean there will be one instance of this variable shared across each class instance, then yes, that's correct. All instances of the class will use that same variable instance.

But keep in mind that with class variables you have to take things like multi-threading into account in many cases, which is a whole different topic.

我ぃ本無心為│何有愛 2024-08-26 06:52:32

来自C++ 编程语言(第 2 版),第 200 页,作者:Bjarne Stroustrup:

除了[普通]函数 (§7.1.2) 和类 (§10.2.4) 之外,不要使用静态

From The C++ Programming Language (2nd edition), page 200, by Bjarne Stroustrup:

Don't use static except inside [plain] functions (§7.1.2) and classes (§10.2.4).

扛刀软妹 2024-08-26 06:52:32

您的示例距离可以编译和测试的内容只有几行:

#include <iostream>
using namespace std;
class Foo {
 public:
    unsigned int bar () {
        static unsigned int counter = 0;
        return counter++;
    }
};

int main ()
{
    Foo a;
    Foo b;

    for (int i=0; i < 10; i++)
      cout<<i<<". "<<a.bar()<<" / "<<b.bar()<<endl;
}

输出如下所示:

0. 1 / 0
1. 3 / 2
2. 5 / 4
3. 7 / 6
4. 9 / 8
5. 11 / 10
6. 13 / 12
7. 15 / 14
8. 17 / 16
9. 19 / 18

所以是的,计数器在所有实例之间共享。

Your example was a couple lines away from being something you could compile and test:

#include <iostream>
using namespace std;
class Foo {
 public:
    unsigned int bar () {
        static unsigned int counter = 0;
        return counter++;
    }
};

int main ()
{
    Foo a;
    Foo b;

    for (int i=0; i < 10; i++)
      cout<<i<<". "<<a.bar()<<" / "<<b.bar()<<endl;
}

The output looks like this:

0. 1 / 0
1. 3 / 2
2. 5 / 4
3. 7 / 6
4. 9 / 8
5. 11 / 10
6. 13 / 12
7. 15 / 14
8. 17 / 16
9. 19 / 18

So yes, the counter is shared across all instances.

心病无药医 2024-08-26 06:52:32

您只需要掌握两件事:

  1. 静态变量存储在执行程序的静态区域中(与全局变量相同)。
  2. 范围受括号的一般规则限制。此外,静态变量具有内部链接。

You just need to grasp two things:

  1. Static variables are stored in static area of the executing program (which is same as that of global variable).
  2. Scope is limited by the general rules of parentheses.Additionally static variables have internal linkage.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文