为什么结构定义有内部链接? [英] Why do structure definitions have internal linkage?

查看:143
本文介绍了为什么结构定义有内部链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我说的是C,但它也适用于类模板C ++)

(I am talking about C, but it also applies to class templates in C++)

在头文件,它是一个自定义把所有的声明,没有定义。然而,我们通常把结构定义或类模板的头文件,以及实际上不知道为什么我们能做到。这并没有真正意义,因为它们的定义,以及 - 一个定义规则。 (是的,结构定义和类模板不会引起任何的存储设置,但你仍然得到重新定义的错误之下,这意味着他们的定义)。

In header file, it is a custom to put all the DECLARATIONS, not definitions. However, we usually put structure definitions or class templates in header file as well without actually knowing why we can. This doesn't really make sense because they are definitions as well -- ONE DEFINITION RULE. (Yes, structure definitions and class templates don't cause any storage to set, but you still get "redefinition" error below which implies that they are definitions).

EX)定义具有相同的文件中相同的标记多个结构给你一个重新定义错误,但定义多个结构在多个源文件相同的标记不会导致任何错误(同样的事情发生与类)。

EX) defining multiple structures with same tag within same file give you a redefinition error, but defining multiple structures with same tag in multiple source files don't cause any error (same thing happens with class).

这是唯一有意义的事情是,结构定义和类模板有内部链接(而非默认的外部链接),但我找不到关于它的K&放任何引用; R或参考手册。
实际上,结构不连在联动提到

The only thing that makes sense is that structure definitions and class templates have internal linkage (opposed to default external linkage), but I can't find any references about it in K&R or reference manual. In fact, structures are not even mentioned in linkage.

我想知道确切的参考,其中ANSI标准指出,这pheonomenon。
(IMO,这是有在ANSI标准某处提到的是pretty模棱两可的东西)。

I want to know the exact reference where ANSI standard points out this pheonomenon. (IMO, this is a pretty ambiguous thing which HAS TO be mentioned in ANSI standard somewhere).

修改
我没有问为什么结构定义可以放进头文件。

EDIT I am NOT asking why structure definitions can be put into the header file.

我问为什么把结构定义在头文件中像它当我们把在头文件变量定义(并将其包含在多个源文件)不会导致重新定义错误

I am asking why putting structure definition in header file won't cause redefinition error like it does when we put variable definitions in header file (and include it in multiple source files)

EX)
test1.c:int类型的= 3;
test2.c中:int类型的= 4;
原因编译,因为重定义的错误。不过,

EX) test1.c: int a = 3; test2.c: int a = 4; Causes compile error because of redefinition. However,

test1.c:结构测试{int类型的};
test2.c中:结构测试{INT B};
不会导致编译错误,唯一的原因,我能想出的是,
结构定义不是有内部链接,或没有联系的。

test1.c: struct test { int a }; test2.c: struct test { int b }; Does not cause compile error, and the only reason I can come up with is that structure definitions either have internal linkage, or no linkage at all.

推荐答案

在仅C对象和函数有联动。由于结构用C可能不包含的功能或静态成员对象在C ++你的问题,使没有太大意义,在这里。

In C only objects and functions have linkage. Since struct in C may not contain functions or "static" member objects as in C++ your question makes not much sense, here.

会员功能,因为它们没有定义,而只是声明中的结构不成问题。如果他们也被定义,它们是在线在线的概念刚刚发明了C ++来捕捉这种情况下:一个函数的定义的,可以通过一个头文件在多个编译单元共享。采纳了这一概念C99(略修改它)。

Member functions in C++ as long as they are not defined but only declared inside the struct pose no problem. If they are also defined, they are inline. The concept of inline was just invented for C++ to capture that case: a function definition that can be shared through a header file in several compilation units. C99 that adopted that concept (modifying it slightly).

静态成员对象构成确实比较的问题。对于如何实例这些家伙的语法是相当模糊的,尤其是对模板 ES或结构秒。如果你想知道这个问题你要问的是,随着C ++特别标记。

static member objects pose indeed more of a problem. The syntax on how to instantiate these guys is quite obscure, especially for template classes or structs. If you'd like to know about that one you'd have to ask for that, tagged specifically with C++.

这篇关于为什么结构定义有内部链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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