【发布时间】:2013-09-25 21:00:37
【问题描述】:
我知道程序中任何地方都可以访问的变量是全局变量。这是一个正确的定义还是我们应该说“在块外声明的变量”? 我试图了解如何更具体地定义它们。我知道在函数外部简单声明全局变量的示例(通常在包含和使用之后)。我知道可以使用带有 extern 关键字的前向声明。 以下是 3 个全局变量(t、d 和 c)的示例:
#include <iostream>
#include "some.h"
using std::cout;
int t;
extern double d;
int main() {
extern char c; // Or here c is not an example of global variable?
t = 3;
cout << t;
}
这是所有情况吗?
【问题讨论】:
-
你也会考虑这个吗?
int& global_int() { static int i = 0; return i; }
标签: c++ global-variables