正确使用'extern'关键字 [英] Using the 'extern' keyword properly
问题描述
有些资料(书籍,在线材料)解释了 extern
的用法,如下所示:
There are sources (books, online materials) that explain the usage of extern
as following:
extern int i; // declaration - has 'extern'
int i = 1; // definition - specified by the absence of 'extern'
有些来源支持以下语法:
And there are sources that support the following syntax:
extern int i; // declaration
extern int i = 1; // definition - specified by the equal sign
// Both marked with 'extern'
我的问题是-这是 C 与 C ++ 的区别,还是 pre-ANSI 与 ANSI的区别练习?
My question is - is this a C vs. C++ distinction, or is it a pre-ANSI vs. ANSI practice?
现在,更实际的问题:
使用第二种语法,我想创建一个全局对象(从每个编译单元可见).构造函数不带参数,因此不需要括号或等号.
Using the second syntax, I want to create a global object (visible from every compilation unit). The constructor takes no parameters, so neither parentheses, nor the equal sign are necessary.
extern MyClass myobject;
现在编译器如何区分声明和定义?
Now how can the compiler make the distinction between a declaration and the definition?
编辑:回到学校后,我习惯了第一种语法(Borland C).后来,我使用了一个编译器(可能是GCC的某些古老版本),该编译器拒绝在没有外部"的情况下编译定义.那就是让我感到困惑的原因.
Back at school, I was used to the first syntax (Borland C). Later I used a compiler (probably some ancient version of GCC) that refused to compile a definition without an 'extern'. That is what made me confused.
推荐答案
专门针对您的示例,此处的C和C ++之间没有区别.两种语言都适用的基本规则是:如果声明中包含 initializer ,则它是定义.时期.不管是否有显式的 extern
.如果它具有初始化程序,则它是定义.
Specifically for your examples, there's no distinction between C and C++ at work here. The basic rule that works in both languages is this: if your declaration includes an initializer, then it is a definition. Period. It does not matter, whether it has explicit extern
in it or not. If it has an initializer, then it is a definition.
这意味着在命名空间范围内, extern int i = 1
和 int i = 1
都是等效的,即,这样声明中的 extern
是多余的.在C ++中,当声明的对象为 const
时,定义中的 extern
变得非冗余,因为默认情况下C ++中的 const
对象具有内部链接.例如, extern const int c = 42;
定义具有外部链接的常量 c
.
That means that in namespace scope both extern int i = 1
and int i = 1
are equivalent, i.e. extern
in such declaration is redundant. In C++ extern
in definitions becomes non-redundant when the declared object is const
, since const
objects in C++ have internal linkage by default. For example, extern const int c = 42;
defines constant c
with external linkage.
如果声明没有初始化程序,则(并且仅在此之前)它开始取决于 extern
关键字的存在.对于 extern
,它是一个未定义的声明.如果没有 extern
,则为定义.(在C语言中,这是一个暂定的定义,但这在我们的上下文中是不重要的.)
If a declaration has no initializer, then (and only then) it begins to depend on the presence of extern
keyword. With extern
it is a non-defining declaration. Without extern
it is a definition. (In C it would be a tentative definition, but that's beside the point in our context).
现在,对于您的实际问题.为了创建全局对象,您必须声明为
Now, for your practical question. In order to create a global object, you have to declare it as
extern MyClass myobject;
(通常将在头文件中完成),然后在某些翻译单元中将其定义为
(which will usually be done in a header file), and then define it in some translation unit as
MyClass myobject;
由于构造函数不接受任何参数,因此这是定义对象的唯一方法.(如果需要,从C ++ 11开始,您还可以使用 MyClass myobject {};
.)
Since your constructor takes no arguments, this is the only way to define your object. (Starting from C++11 you can also use MyClass myobject{};
if you so desire.)
如果您必须向构造函数提供参数(例如 42
),则可以同时使用两者
If you had to supply arguments to the constructor (say, 42
), you would be able to use both
MyClass myobject(42);
和
extern MyClass myobject(42);
作为定义,因为存在初始化程序可确保确实将其解释为定义.
as definition, since presence of an initializer ensures that it is indeed interpreted as a definition.
这篇关于正确使用'extern'关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!