我可以包含全局静态成员吗?
头文件之一中很少有静态的全局变量。我看到这些变量在关联的 .cc 文件中使用。所以,看起来这没有问题。
我的问题是:
包含全局变量与静态全局变量有什么区别? 我知道静态全局在其文件之外没有可见性。但不知道当它作为 #included 的 .h 的一部分时它会如何工作。
我编写了一个示例程序,并尝试了同样的事情。但是,当我将变量设置为静态时,我会遇到编译错误。当它只是全球性的时候,就很好了。 那么,我在常规 g++ 构建中是否缺少一些东西? (请注意,最初的案例是在我们的官方代码库上,它有足够的 makefile、.h 文件等)。
感谢您的帮助!
这是我的示例程序:
.h 文件:
#include <iostream>
typedef unsigned int uint;
static const int appk=189;
class abc1
{
public:
abc1(int x);
virtual void printVal();
};
.cc 文件:
#include "abc1.h"
extern int appk;
abc1::abc1(int x)
{
}
void abc1::printVal()
{
printf("abc1 print: %d\n", appk);
}
There are few global variables which are static in one of the header files. I see these variables are used in the associated .cc files. So, looks like this has no issues.
My questions are:
Whats the difference between including a global variable vs static global variable ?
I know static global doesnt have visibility outside its file. But dont know how this would work when it comes as part of a .h which is #included.I wrote a sample program, and tried the same thing. But, I get compilation error the moment I make the variable static. When it is just global, it is fine.
So, is there something which I am missing on a regular g++ build ? (Please note, the initial case was on our official code base which has enough makefiles, .h files and all).
Thanks for the help !
Here is my sample program :
.h file:
#include <iostream>
typedef unsigned int uint;
static const int appk=189;
class abc1
{
public:
abc1(int x);
virtual void printVal();
};
.cc file:
#include "abc1.h"
extern int appk;
abc1::abc1(int x)
{
}
void abc1::printVal()
{
printf("abc1 print: %d\n", appk);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(1) 如果将全局变量放在
.h
文件中并将其包含在各个 .cpp/.cc 文件中,则每个文件都会多次定义该变量。所以你最有可能遇到链接器错误。为了克服这个问题,大多数情况下您可能会使用
extern
关键字:并仅在一个翻译单元中定义它:
(2) 如果您将
static
全局变量放入.h
文件并包含它,那么您将不会收到任何错误,因为对于每个不同的翻译单元,该静态
全局都会有一个不同的副本多变的。但是,这种用法已被弃用;相反,最好使用未命名的
namespace
:从您的问题来看,我认为您不应该收到
static
全局的任何链接器错误。(1) If you put a global variable in a
.h
file and include it in various .cpp/.cc files then it will be defined multiple times for every file. So you are most like to get a linker error.To overcome that, mostly you are likely to use
extern
keyword:and define that in only one translation unit:
(2) If you put a
static
global in a.h
file and include it, then you will not get any error, because for every different translation unit, there will be a different copy for thatstatic
global variable.However, such usage is deprecated; instead of that it's better to use unnamed
namespace
:From your question, I don't see that you should get any linker error for
static
global.没有代码很难告诉你的编译错误,但是如果你有一个声明静态全局的标头,那么你只需在包含该标头的每个翻译单元中独立且单独地创建该全局变量。
示例:
header.h:
file1.cpp:
file2.cpp:
这两个文件都可以访问名为
a
的全局变量,但每个文件都有自己单独的副本,对其翻译单元私有,该副本外面看不到。举一个实际的例子,我认为
cout
被声明为静态全局变量,因此每个使用
的人都会得到自己的副本。Hard to tell your compilation error without code, but if you have a header that declares a static global, then you just create that global variable independently and separately in each translation unit that includes the header.
Example:
header.h:
file1.cpp:
file2.cpp:
The two files both have access to a global variable called
a
, but each file has its own separate copy, private to its translation unit, which is not visible outside.For a practical example, I think
cout
is declared as a static global, so everyone who uses<iostream>
gets their own copy.static
变量具有内部链接。这意味着,如果您在xh
中有一个静态变量a
,并且将xh
包含在两个文件中,例如m.cpp< /code> 和
n.pp
那么这两个文件中的每一个都会获得自己的a
副本,这意味着如果您在m.cpp
中更改其值>,那么n.cpp
将看不到这一点更改,因为每个翻译单元(.cpp)中存在两个同名变量。而且它们彼此独立。但是如果
a
不是静态的,那么在多个文件中包含xh
,就会出现多重定义错误,因为每次包含xh
将尝试定义a
,但由于a
不是静态的;它现在具有外部链接,这意味着如果它在m.cpp
中定义,那么在n.cpp
中包含xh
时会出现错误(或反之亦然)。在这种情况下,您必须将xh
编写为:然后在一个
.cpp
文件中定义a
,或者是m.cpp。 cpp
或n.cpp
,但不能同时使用两者。说出它的m.cpp
。你就完成了。现在,您可以将
xh
包含在任意数量的.cpp
文件中,并且可以访问a
、修改其值,做任何您想做的事情。现在,所有.cpp
文件都可以看到对其进行的任何更改。static
variable has internal-linkage. What it means is that if you have a static variablea
inx.h
and you includex.h
in two files saym.cpp
andn.pp
then each of these two files gets its own copy ofa
which means if you change its value inm.cpp
, thenn.cpp
is not going to see that change, because there exists two variables with same name in each translation unit (.cpp). And they're independent of each other.But if
a
is not static, then includingx.h
in more than one files, you will get multiple-definition error, because each inclusion ofx.h
will try to definea
, but sincea
is not static; it has external linkage now, which means if its defined inm.cpp
, then you will get error when includingx.h
inn.cpp
(or vice-versa). In this case, you've to writex.h
as:And then define
a
in exactly one.cpp
file, eitherm.cpp
orn.cpp
, but not both. Say itsm.cpp
.And you're done. Now you can include
x.h
in as many.cpp
file as you want, and can accessa
, modify its value, do whatever you want. Any change to it, will be seen by all.cpp
files now.