加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

任何人都可以解释这部分C 0x草案标准吗?

发布时间:2020-12-16 09:33:40 所属栏目:百科 来源:网络整理
导读:来自ISO标准草案:§3.0/ 9: n3234说: A name used in more than one translation unit can potentially refer to the same entity in these translationunits depending on the linkage (3.5) of the name specified in each translation unit. 有人可以
来自ISO标准草案:§3.0/ 9:

n3234说:

A name used in more than one translation unit can potentially refer to the
same entity in these translationunits depending on the linkage (3.5) of the
name specified in each translation unit.

有人可以用一个例子解释这一点吗?

这句话到底在说什么?任何人都可以用程序来证明这一点吗?

解决方法

当然!这说的是,如果你有多个源文件(翻译单元)都使用某个名称(例如,变量,类或函数的名称),那么这些不同的文件可能正在讨论相同的变量,类或函数,假设该实体的声明方式使其在不同文件中可见(即,取决于其链接).

例如,如果我有这个文件:

A.cpp:

int globalInt;

int main() {
     globalInt = 137;
}

这一个在这里:

B.cpp:

extern int globalInt;

void foo() {
    globalInt = 42;
}

然后在两个文件中,名称globalInt引用在A.cpp中定义的全局int变量globalInt.这就是这一点.

但请注意,如果我在没有外部链接的情况下声明globalInt,那么这两个文件将讨论不同的变量.例如,在这种情况下:

C.cpp:

static int globalInt;

int main() {
     globalInt = 137;
}

D.cpp:

static int globalInt;

void foo() {
    globalInt = 42;
}

现在,C.cpp中引用的globalInt变量与D.cpp中的变量不同,即使它们具有相同的名称.

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读