我可以包含全局静态成员吗​​?

发布于 2024-11-29 06:58:22 字数 738 浏览 1 评论 0原文

头文件之一中很少有静态的全局变量。我看到这些变量在关联的 .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 技术交流群。

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

发布评论

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

评论(3

笑看君怀她人 2024-12-06 06:58:22

(1) 如果将全局变量放在 .h 文件中并将其包含在各个 .cpp/.cc 文件中,则每个文件都会多次定义该变量。所以你最有可能遇到链接器错误
为了克服这个问题,大多数情况下您可能会使用 extern 关键字:

// myfile.h
extern int i;

并仅在一个翻译单元中定义它:

// somefile.cc
int i;

(2) 如果您将 static 全局变量放入 .h 文件并包含它,那么您将不会收到任何错误,因为对于每个不同的翻译单元,该静态全局都会有一个不同的副本多变的。

// myfile.h
static int i;  // creates a unique and unrelated copy in all .cc file where included

但是,这种用法已被弃用;相反,最好使用未命名的 namespace

namespace {
  int i;
}

从您的问题来看,我认为您不应该收到 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:

// myfile.h
extern int i;

and define that in only one translation unit:

// somefile.cc
int i;

(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 that static global variable.

// myfile.h
static int i;  // creates a unique and unrelated copy in all .cc file where included

However, such usage is deprecated; instead of that it's better to use unnamed namespace:

namespace {
  int i;
}

From your question, I don't see that you should get any linker error for static global.

甜点 2024-12-06 06:58:22

没有代码很难告诉你的编译错误,但是如果你有一个声明静态全局的标头,那么你只需在包含该标头的每个翻译单元中独立且单独地创建该全局变量。

示例:

header.h:

#ifndef H_XXX
#define H_XXX

static int a;

#endif

file1.cpp:

#include "header.h"

// now have access to a variable called "a"

file2.cpp:

#include "header.h"

// now also have access to some "a"

这两个文件都可以访问名为 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:

#ifndef H_XXX
#define H_XXX

static int a;

#endif

file1.cpp:

#include "header.h"

// now have access to a variable called "a"

file2.cpp:

#include "header.h"

// now also have access to some "a"

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.

弃爱 2024-12-06 06:58:22

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 编写为:

//x.h
extern int a;

然后在一个 .cpp 文件中定义 a,或者是 m.cpp。 cppn.cpp,但不能同时使用两者。说出它的m.cpp

//m.cpp
#include "x.h"

int a =10;

你就完成了。现在,您可以将 xh 包含在任意数量的 .cpp 文件中,并且可以访问 a、修改其值,做任何您想做的事情。现在,所有 .cpp 文件都可以看到对其进行的任何更改。

static variable has internal-linkage. What it means is that if you have a static variable a in x.h and you include x.h in two files say m.cpp and n.pp then each of these two files gets its own copy of a which means if you change its value in m.cpp, then n.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 including x.h in more than one files, you will get multiple-definition error, because each inclusion of x.h will try to define a, but since a is not static; it has external linkage now, which means if its defined in m.cpp, then you will get error when including x.h in n.cpp (or vice-versa). In this case, you've to write x.h as:

//x.h
extern int a;

And then define a in exactly one .cpp file, either m.cpp or n.cpp, but not both. Say its m.cpp.

//m.cpp
#include "x.h"

int a =10;

And you're done. Now you can include x.h in as many .cpp file as you want, and can access a, modify its value, do whatever you want. Any change to it, will be seen by all .cpp files now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文