简体   繁体   English

这里需要extern关键字,const在cpp文件中

[英]Is extern keyword required here, const in cpp file

If I have in header file 如果我有头文件

namespace Bob
{
    extern const T x;
};

And in source file 并在源文件中

extern const T Bob::x = 123;

Is the second extern in the source file required or optional? 源文件中的第二个extern是必需的还是可选的?

I've searched and found conflicting information. 我搜索过并发现了相互矛盾的信息。

From this webpage: http://msdn.microsoft.com/en-us/library/357syhfh.aspx 从这个网页: http//msdn.microsoft.com/en-us/library/357syhfh.aspx

But to get the same behavior in C++, you must declare your const variable [in source file] as: 但是要在C ++中获得相同的行为,必须将const变量[在源文件中]声明为:

extern const int i = 2;

Usually, the extern keyword tells the compiler not to define a symbol, because it will be defined somewhere else. 通常, extern关键字告诉编译器不要定义符号,因为它将在其他地方定义。 So writing eg 所以写作例如

namespace Bob {
    extern T x;
}

does not define the variable x , but rather declares it. 没有定义变量x ,而是声明它。 You can have as many extern declarations as you like. 您可以拥有任意数量的extern声明。 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. 在代码中的某个地方,以提供定义。

The const keyword is a little special here, because it implies internal linkage. const关键字在这里有点特殊,因为它意味着内部链接。 This means, that the definition of x will not be visible outside the specific compilation unit where it was defined. 这意味着, x的定义在定义它的特定编译单元之外是不可见的。 To alter this behavior you do need to write 要改变这种行为,你需要写

    extern const T Bob::x = 123;

if you want x to be const and also reference it from other compilation units. 如果你想要xconst并且也从其他编译单元引用它。

----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 . 只是绝对清楚:如果一个const变量应该在其编译单元之外引用,那么你必须明确地声明它是extern

However, if the declaration is given separately from the definition, then the definition does not necessarily need to specify the keyword extern again. 但是,如果声明与定义分开给出,则定义不一定需要再次指定关键字extern Yet another example for demonstration: 示范的又一个例子:

myheader.h myheader.h

extern const int i;

This declares i a const integer with external linkage, but does not define it. 此声明i一个const具有外部链接的整数,但没有定义它。

main.cpp, version 1 main.cpp,版本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, version 2 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. 我希望这一切现在对你有意义。

You can control the linkage of a symbol with the static and extern keywords. 您可以使用staticextern关键字控制符号的链接。 The default linkage is extern for non-const symbols and static (ie internal) for const symbols at namespace scope. 对于非const符号,默认链接是extern对于命名空间范围的const符号,默认链接是static (即内部)。 This is valid for C++ but not for C (where you would have to declare it as static to make it have internal linkage). 这对C ++有效,但对C不适用(你必须将它声明为static以使其具有内部链接)。

So if you have 所以,如果你有

const int i = 2;

into a .c file and you use it into another unit by declaring 进入.c文件,然后通过声明将其用于另一个单元

extern const int i;

that's fine in C since that variable has external linkage by default (and with the extern keyword you're instructing the compiler to find it into another unit), but in C++ the same 这在C中很好,因为该变量默认具有外部链接(并且使用extern关键字指示编译器将其发现到另一个单元中),但在C ++中也是如此

const int i = 2;

into a .cpp file (firmstanding what I wrote) has internal linkage and it would have to be defined with 进入一个.cpp文件(坚定我写的内容)有内部链接,它必须定义

extern const int i = 2;

to explicitly have external linkage and be able to be used into another unit with 显式拥有外部链接,并能够用于另一个单位

extern const int i;

Finally when you initialize a global variable you're always defining it in the module where you're doing it, this means that the following is perfectly valid: 最后,当您初始化一个全局变量时,您总是在您正在执行它的模块中定义它,这意味着以下内容完全有效:

#include <iostream>
using namespace std;

#define T int

extern const T x; // This is a declaration
extern const T x = 123; // This is a definition

int main() {

  std::cout << x; // 123

  return 0;
}

That means the second 'extern' keyword is also unnecessary. 这意味着第二个'extern'关键字也是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM