extern关键字真的有必要吗? [英] Is extern keyword really necessary?

查看:284
本文介绍了extern关键字真的有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ... 
#includetest1.h

int main(..)
{
count << aaa << endl;
}

test1.h ,我没有使用extern关键字,但仍然可以引用 aaa

所以我怀疑 extern 真的有必要吗?

解决方案

extern 具有其用途。但它主要涉及被皱眉的全局变量。 extern 背后的主要思想是用外部链接声明事物。因此,它与 static 的相反。但外部链接在许多情况下是默认链接,所以你不需要 extern 在这些情况下。 extern 的另一个用法是:它可以将定义转换为声明。示例:

  extern int i; //具有外部链接的i的声明
//(只告诉编译器存在i)

int i; //用外部链接定义i
//(实际上保留内存,不应该在头文件中)

const int f = 3; //定义f的内部链接(由于const)
//(这适用于C ++而不是C.在C中有
//外部链接。)在C ++中,把
//这样的东西放到头文件中。

extern const int g; //用外部链接声明g
//可以放在头文件中

extern const int g = 3; //用外部链接定义g
//不应该在头文件中

static int t; //具有内部连接的t的定义。
//可能出现在任何地方。每个翻译单元
//有这样一行有它自己的t对象。

你看,这很复杂。有两个正交的概念:链接(外部vs内部)和声明与定义的事情。 extern 关键字可以同时影响两者。关于链接,它与 static 的相反。但是 static 的含义也是重载的,并且取决于上下文 - 是或不控制链接。另一件事是控制对象的生命周期(静态生命周期)。但在全局范围,所有变量已经有一个静态的生命时间,有些人认为循环使用关键字控制连接是一个好主意(这是我只是猜测)。



链接基本上是在namespace scope上声明/定义的对象或函数的属性。如果它具有内部链接,则不能通过其他翻译单元的名称直接访问。如果它有外部链接,在所有翻译单元中只有一个定义(除了例外,参见一个定义规则)。


...
#include "test1.h"

int main(..)
{
    count << aaa <<endl;
}

aaa is defined in test1.h,and I didn't use extern keyword,but still can reference aaa.

So I doubt is extern really necessary?

解决方案

extern has its uses. But it mainly involves "global variables" which are frowned upon. The main idea behind extern is to declare things with external linkage. As such it's kind of the opposite of static. But external linkage is in many cases the default linkage so you don't need extern in those cases. Another use of extern is: It can turn definitions into declarations. Examples:

extern int i;  // Declaration of i with external linkage
               // (only tells the compiler about the existence of i)

int i;         // Definition of i with external linkage
               // (actually reserves memory, should not be in a header file)

const int f = 3; // Definition of f with internal linkage (due to const)
                 // (This applies to C++ only, not C. In C f would have
                 // external linkage.) In C++ it's perfectly fine to put
                 // somethibng like this into a header file.

extern const int g; // Declaration of g with external linkage
                    // could be placed into a header file

extern const int g = 3; // Definition of g with external linkage
                        // Not supposed to be in a header file

static int t; // Definition of t with internal linkage.
              // may appear anywhere. Every translation unit that
              // has a line like this has its very own t object.

You see, it's rather complicated. There are two orthogonal concepts: Linkage (external vs internal) and the matter of declaration vs definition. The extern keyword can affect both. With respect to linkage it's the opposite of static. But the meaning of static is also overloaded and -- depending on the context -- does or does not control linkage. The other thing it does is to control the life-time of objects ("static life-time"). But at global scope all variables already have a static life-time and some people thought it would be a good idea to recycle the keyword for controlling linkage (this is me just guessing).

Linkage basically is a property of an object or function declared/defined at "namespace scope". If it has internal linkage, it won't be directly accessible by name from other translation units. If it has external linkage, there shall be only one definition across all translation units (with exceptions, see one-definition-rule).

这篇关于extern关键字真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆