在 C++ 中的多个文件中使用相同的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2544852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:52:15  来源:igfitidea点击:

Using the same variable across multiple files in C++

c++variables

提问by Sam152

In the process of changing some code, I have spilt some functions into multiple files. I have the files controls.cppand display.cppand I would like to be able to have access to the same set of variables in both files. I don't mind where they are initialized or declared, as long as the functions in both files can use them.

在更改一些代码的过程中,我将一些功能溢出到多个文件中。我有这些文件controls.cppdisplay.cpp并且我希望能够访问两个文件中的同一组变量。我不介意它们在哪里初始化或声明,只要两个文件中的函数都可以使用它们。

This was not an issue when the functions were in the same file, but now it seems almost impossible after an hour of googling and trying various things.

当函数在同一个文件中时,这不是问题,但现在经过一个小时的谷歌搜索和尝试各种事情后,这似乎几乎不可能。

回答by codaddict

Define the variable in one file like:

在一个文件中定义变量,如:

type var_name;

And declare it global in the other file like:

并在另一个文件中将其声明为全局的,例如:

extern type var_name;

回答by Mihir Mehta

use those variables as externi.e.

使用这些变量作为externie

extern int i;

in another file declare same as normal global variable...

在另一个文件中声明与普通全局变量相同...

int i;//global

回答by quamrana

Create two new files:

创建两个新文件:

  1. Something like Globals.hand declare all variables like: extern type name;
    • btw remember include guards.
  2. Something like Globals.cppand declare variables like: type name;
  1. 类似于Globals.h并声明所有变量,例如:extern type name;
    • 顺便说一句,记住包括警卫。
  2. 类似于Globals.cpp并声明变量,例如:type name;

Then add #include "Globals.h"at the top of:

然后#include "Globals.h"在顶部添加:

  • Globals.cpp
  • controls.cpp
  • display.cpp
  • Globals.cpp
  • controls.cpp
  • display.cpp

You may then want some functions to initialise them.

然后您可能需要一些函数来初始化它们。

回答by BrainSlugs83

Essentially all you have to do is declare the variable once in one code file, and declare it in the others as extern (making sure NOT to initialize it when you are declaring it extern, or some compilers will ignore the extern keyword, giving you compiler errors.)

基本上你所要做的就是在一个代码文件中声明一次变量,然后在其他文件中将它声明为 extern(确保在声明它时不要初始化它 extern,否则一些编译器会忽略 extern 关键字,给你编译器错误。)

The simplest way to do this is to use a macro in a header file, like such:

最简单的方法是在头文件中使用宏,例如:

#pragma once

#ifdef __MAIN__
    #define __EXTERN(type, name, value)     type name = value
#else
    #define __EXTERN(type, name, value)     extern type name;
#endif

and then declare your variables in that same header file, like such:

然后在同一个头文件中声明变量,如下所示:

__EXTERN(volatile int,  MyVolatileInteger,  0);

from any ONE file in the project, include the header file, like such:

从项目中的任何一个文件,包括头文件,像这样:

#define __MAIN__
#include "Globals.h"

from all the rest, just simply include it normally, like such:

其余的,只需简单地包含它,就像这样:

#include "Globals.h"

Presto, you're done. Variables only declared once, and initialized in-line. This is very maintainable, and saves you the trouble of having to declare everything twice.

Presto,你完成了。变量只声明一次,并内联初始化。这是非常易于维护的,并且省去了必须两次声明所有内容的麻烦。

回答by simong

All of the declarations you want visible in multiple compilation units (.cpp files), should go into a header file that you include in all places that need to use the variable, type, class, etc.

您希望在多个编译单元(.cpp 文件)中可见的所有声明都应放入一个头文件中,该头文件包含在需要使用变量、类型、类等的所有位置。

This is vastly better than extern, which essentially hides your intention to share the declaration.

这比 extern 好得多,后者基本上隐藏了您分享声明的意图。