在全局范围内使用静态变量和函数

时间:2021-07-25 20:04:44

Is there a use for flagging a variable as static, when it lies in the global scope of a .cpp file, not in a function?

当一个变量位于.cpp文件的全局范围内而不是函数中时,是否可以将变量标记为静态?

Can you use the static keyword for functions as well? If yes, what is their use?

你也可以为函数使用static关键字吗?如果是,他们的用途是什么?

3 个解决方案

#1


14  

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

在这种情况下,关键字static表示函数或变量只能由同一cpp文件中的代码使用。相关符号不会被导出,也不会被其他模块使用。

This is good practice to avoid name *ing in big software when you know your global functions or variables are not needed in other modules.

当您知道其他模块中不需要全局函数或变量时,这是避免大软件中名称冲突的好习惯。

#2


17  

Yes, if you want to declare file-scope variable, then static keyword is necessary. static variables declared in one translation unit cannot be referred to from another translation unit.

是的,如果要声明文件范围变量,则需要使用static关键字。在一个翻译单元中声明的静态变量不能从另一个翻译单元引用。


By the way, use of static keyword is deprecated in C++03.

顺便说一句,在C ++ 03中不推荐使用static关键字。

The section $7.3.1.1/2 from the C++ Standard (2003) reads,

C ++标准(2003)的第7.3.1.1 / 2节中的内容如下:

The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative.

在声明命名空间作用域中的对象时,不推荐使用static关键字; unnamed-namespace提供了一个更好的选择。

C++ prefers unnamed namespace over static keyword. See this topic:

C ++比静态关键字更喜欢未命名的命名空间。看到这个主题:

Superiority of unnamed namespace over static?

未命名的命名空间优于静态?

#3


1  

Taking as an example -

以身作为例 -

// At global scope
int globalVar; // Equivalent to static int globalVar;
               // They share the same scope
               // Static variables are guaranteed to be initialized to zero even though
               //    you don't explicitly initialize them.


// At function/local scope

void foo()
{
    static int staticVar ;  // staticVar retains it's value during various function
                            // function calls to foo();                   
}

They both cease to exist only when the program terminates/exits.

它们只有在程序终止/退出时才会停止存在。

#1


14  

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

在这种情况下,关键字static表示函数或变量只能由同一cpp文件中的代码使用。相关符号不会被导出,也不会被其他模块使用。

This is good practice to avoid name *ing in big software when you know your global functions or variables are not needed in other modules.

当您知道其他模块中不需要全局函数或变量时,这是避免大软件中名称冲突的好习惯。

#2


17  

Yes, if you want to declare file-scope variable, then static keyword is necessary. static variables declared in one translation unit cannot be referred to from another translation unit.

是的,如果要声明文件范围变量,则需要使用static关键字。在一个翻译单元中声明的静态变量不能从另一个翻译单元引用。


By the way, use of static keyword is deprecated in C++03.

顺便说一句,在C ++ 03中不推荐使用static关键字。

The section $7.3.1.1/2 from the C++ Standard (2003) reads,

C ++标准(2003)的第7.3.1.1 / 2节中的内容如下:

The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative.

在声明命名空间作用域中的对象时,不推荐使用static关键字; unnamed-namespace提供了一个更好的选择。

C++ prefers unnamed namespace over static keyword. See this topic:

C ++比静态关键字更喜欢未命名的命名空间。看到这个主题:

Superiority of unnamed namespace over static?

未命名的命名空间优于静态?

#3


1  

Taking as an example -

以身作为例 -

// At global scope
int globalVar; // Equivalent to static int globalVar;
               // They share the same scope
               // Static variables are guaranteed to be initialized to zero even though
               //    you don't explicitly initialize them.


// At function/local scope

void foo()
{
    static int staticVar ;  // staticVar retains it's value during various function
                            // function calls to foo();                   
}

They both cease to exist only when the program terminates/exits.

它们只有在程序终止/退出时才会停止存在。