这里需要extern关键字,cpp文件中的const [英] Is extern keyword required here, const in cpp file
问题描述
如果我有头文件
namespace Bob
{
extern const T x;
};
并在源文件中
extern const T Bob::x = 123;
是源文件中的第二个 extern
必要或可选?
Is the second extern
in the source file required or optional?
我搜索过并发现了冲突的信息。
I've searched and found conflicting information.
b http://msdn.microsoft.com/en-us/library/357syhfh。 aspx
但是为了在C ++中得到相同的行为,你必须在源文件中声明你的const变量: / p>
But to get the same behavior in C++, you must declare your const variable [in source file] as:
extern const int i = 2;
推荐答案
通常, extern
关键字告诉编译器不要定义一个符号,因为它将被定义在别的地方。例如
Usually, the extern
keyword tells the compiler not to define a symbol, because it will be defined somewhere else. So writing e.g.
namespace Bob {
extern T x;
}
未定义变量 x
,而是声明它。您可以根据需要拥有尽可能多的 extern
声明。但是,如果您不提供定义,链接将失败。所以你必须定义
does not define the variable x
, but rather declares it. You can have as many extern
declarations as you like. However, if you do not provide a definition the linking will fail. So you have to define
T Bob::x;
在代码中的某处提供定义。
somewhere in the code in order to provide the definition.
const
关键字在这里有点特别,因为它意味着内部链接。这意味着 x
的定义在定义它的特定编译单元之外是不可见的。要改变这种行为,你需要写
The const
keyword is a little special here, because it implies internal linkage. This means, that the definition of x
will not be visible outside the specific compilation unit where it was defined. To alter this behavior you do need to write
extern const T Bob::x = 123;
如果您要 x
为 const
并从其他编译单元中引用它。
if you want x
to be const
and also reference it from other compilation units.
----另一个编辑
绝对清楚:
如果一个 const
变量应该在其编译单元之外被引用,那么您必须显式声明 extern
。
----yet another edit----
Just to be absolutely clear:
If a const
variable is supposed to be referenced outside of its compilation unit, then you must explicitly declare it extern
.
与定义分开,则定义不一定需要再次指定关键字 extern
。另一个演示示例:
However, if the declaration is given separately from the definition, then the definition does not necessarily need to specify the keyword extern
again. Yet another example for demonstration:
myheader.h
extern const int i;
这会声明 i
a const 整数与外部链接,但不定义它。
This declares i
a const
integer with external linkage, but does not define it.
main.cpp,版本1 / p>
main.cpp, version 1
#include "myheader.h" //Through the include, the above declaration of `i` comes before its definition.
const int i=123; // Although the `extern` modifier is omitted here,
// it's still in effect because we already declared `i` as `extern`
// Therefore it makes no difference here, whether or not you specify `extern` again.
// The compiler knows this is a definition either way, because of the initialization.
main.cpp,版本2
//#include "myheader.h"
extern const int i=123; // this line is declaration and definition in one, because we did not include
// the declaration from myheader.h. Thus, to tell the compiler that you want
// `i` with external linkage, you MUST specify `extern`. Otherwise you'll get
// internal linkage.
我希望这一切对你来说都有意义。
I hope all this now makes sense to you.
这篇关于这里需要extern关键字,cpp文件中的const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!