C ++,静态局部变量(方法)与全局变量(文件)之间有什么区别? [英] C++, What is difference between static local variable(method) vs global(file) variable?

查看:70
本文介绍了C ++,静态局部变量(方法)与全局变量(文件)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 一次创建本地 static 变量,该方法将超出范围,但是 static 变量不会被破坏,并且它将一直存在于内存中,直到程序结束
  2. 全局 static 变量一直存在于内存中,直到程序结束.
  1. A local static variable is created once and the method will go out of scope, but the static variable is not destroyed and it will live in memory until the program ends.
  2. A global static variable live in memory until the program ends.

那么本地和全局 static 变量之间有什么区别?

So what is difference between local and global static variables?

何时应使用局部 static 变量和全局 static 变量?

When should we use local static variables and global static variables?

推荐答案

其他答案告诉您本地静态对象(对于函数而言本地)和非本地静态对象(声明为全局或在命名空间中声明的静态对象);但是,您还应该了解在一个之上使用一个之上的重要性.

Other answers have told you the differences between a local static object (local to a function) and a non-local static object (static objects declared global or in a namespace); however, you should also understand the importance of using one over the other.

我在这里使用的信息来自有效的C ++第三版斯科特·迈尔斯(Scott Myers)(推荐和出色阅读)

The information I use here comes from Effective C++ Third Edition by Scott Myers (a recommended and excellent read)

静态对象是指从其构造开始到程序结束为止的对象.

A static object is one that exists from the time it's constructed until the end of the program.

正如其他人所提到的,非本地静态对象是在 main 之前构造的,而 local静态对象是在第一次函数被调用.

As others have mentioned, a non-local static object is constructed before main whereas a local static object is constructed the first time the function is called.

但是,如果您有两个静态对象而一个依赖另一个,该怎么办.您如何确定一个要比另一个更先构建?您不能:未定义在不同翻译单元中定义的非本地静态对象的相对顺序

But what if you had two static objects and one relied on the other. How could you be sure that one would be constructed before the other? You can't: The relative order of initialisation of non-local static objects defined in different translation units is undefined

斯科特·迈尔斯(Scott Myers)对翻译单位的定义(来自上述书籍)

Scott Myers definition of a translation unit (from aforementioned book):

翻译单元是产生单个目标文件的源代码.它基本上是一个源文件,再加上所有的#include文件.

A translation unit is the source code giving rise to a single object file. It's basically a single source file, plus all of the #include files.

因此,假设您在单独的源文件中有这两个非本地静态对象,则不能保证其中一个将在另一个之前被构造.这是本地静态对象占主导的地方!

So imagine you have these two non-local static objects in separate source files, you couldn't guarantee one would be constructed before the other. This is where a local static object prevails!

考虑Scott Myers的 FileSystem 类和 Directory 类的示例,其中 Directory 依赖于 FileSystem :

Consider Scott Myers' example of a FileSystem class and a Directory class, where the Directory relies on the FileSystem:

class FileSystem
{
public:
    size_t numDisks() const;
};
extern FileSystem tfs;

class Directory
{
public:
    Directory()
    {
        size_t disks = tfs.numDisks();
        ...
    }
};

如果在 FileSystem 之前构造了 Directory ,那么您将使用未初始化的类!幸运的是,您可以通过使用本地静态对象来解决此问题!

If Directory gets constructed before FileSystem, then you are going to be using an uninitialised class! Fortunately, you can get around this by using local static objects!

class FileSystem
{
public:
    size_t numDisks() const;
};
FileSystem& tfs()
{
    static FileSystem fs;
    return fs;
}

现在,当构建 Directory 时,即使它是在 FileSystem 被构建之前构建的,也可以保证 FileSystem 被构建为在使用之前构造!

Now when Directory is constructed, even if it's constructed before FileSystem is, FileSystem is guaranteed to be constructed before it's used!

这篇关于C ++,静态局部变量(方法)与全局变量(文件)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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