何时在 C++ 中使用 extern
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10422034/
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
When to use extern in C++
提问by Aslan986
I'm reading "Think in C++" and it just introduced the extern
declaration. For example:
我正在阅读“用 C++ 思考”,它刚刚介绍了extern
声明。例如:
extern int x;
extern float y;
I think I understand the meaning (declaration without definition), but I wonder when it proves useful.
我想我理解它的意思(没有定义的声明),但我想知道它什么时候证明有用。
Can someone provide an example?
有人可以提供一个例子吗?
回答by dreamlax
This comes in useful when you have global variables. You declare the existenceof global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source files.
当您有全局变量时,这很有用。您在头文件中声明全局变量的存在,以便包含该头文件的每个源文件都知道它,但您只需在其中一个源文件中“定义”一次。
To clarify, using extern int x;
tells the compiler that an object of type int
called x
exists somewhere. It's not the compilers job to know where it exists, it just needs to know the type and name so it knows how to use it. Once all of the source files have been compiled, the linker will resolve all of the references of x
to the one definition that it finds in one of the compiled source files. For it to work, the definition of the x
variable needs to have what's called “external linkage”, which basically means that it needs to be declared outside of a function (at what's usually called “the file scope”) and without the static
keyword.
澄清extern int x;
一下, using告诉编译器在某处存在一个int
称为类型的对象。知道它在哪里存在不是编译器的工作,它只需要知道类型和名称,以便知道如何使用它。编译完所有源文件后,链接器将解析对它在已编译源文件之一中找到的一个定义的所有引用。为了让它工作,变量的定义需要有所谓的“外部链接”,这基本上意味着它需要在函数之外(通常称为“文件范围”)声明,并且没有关键字。x
x
x
static
header:
标题:
#ifndef HEADER_H
#define HEADER_H
// any source file that includes this will be able to use "global_x"
extern int global_x;
void print_global_x();
#endif
source 1:
来源 1:
#include "header.h"
// since global_x still needs to be defined somewhere,
// we define it (for example) in this source file
int global_x;
int main()
{
//set global_x here:
global_x = 5;
print_global_x();
}
source 2:
来源2:
#include <iostream>
#include "header.h"
void print_global_x()
{
//print global_x here:
std::cout << global_x << std::endl;
}
回答by MByD
It is useful when you share a variable between a few modules. You define it in one module, and use extern in the others.
当您在几个模块之间共享一个变量时,它很有用。您在一个模块中定义它,并在其他模块中使用 extern。
For example:
例如:
in file1.cpp:
在 file1.cpp 中:
int global_int = 1;
in file2.cpp:
在 file2.cpp 中:
extern int global_int;
//in some function
cout << "global_int = " << global_int;
回答by Trevor
It's all about the linkage.
这都是关于链接的。
The previous answers provided good explainations about extern
.
之前的答案提供了关于extern
.
But I want to add an important point.
但我想补充一点。
You ask about extern
in C++not in Cand I don't know why there is no answer mentioning about the case when extern
comes with const
in C++.
你问extern
在C ++不ç,我不知道为什么没有答案提这个案子时,extern
与来自const
于C ++。
In C++, a const
variable has internal linkage by default (not like C).
在 C++ 中,const
变量默认具有内部链接(不像 C)。
So this scenario will lead to linking error:
所以这种情况会导致链接错误:
Source 1 :
来源 1:
const int global = 255; //wrong way to make a definition of global const variable in C++
Source 2 :
来源 2:
extern const int global; //declaration
It need to be like this:
它需要是这样的:
Source 1 :
来源 1:
extern const int global = 255; //a definition of global const variable in C++
Source 2 :
来源 2:
extern const int global; //declaration
回答by Marlon
This is useful when you want to have a global variable. You define the global variables in some source file, and declare them extern in a header file so that any file that includes that header file will then see the same global variable.
当您想要一个全局变量时,这很有用。您在某个源文件中定义全局变量,并在头文件中将它们声明为 extern,以便任何包含该头文件的文件都将看到相同的全局变量。