在头文件中定义类时,为什么没有多重定义错误? [英] Why is there no multiple definition error when you define a class in a header file?

查看:204
本文介绍了在头文件中定义类时,为什么没有多重定义错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确提出了这个问题,但请允许我解释一下.

I'm not sure if I asked the question correctly, but let me explain.

首先,我阅读了这篇文章,解释了声明和定义之间的区别: http://www.cprogramming.com/declare_vs_define.html

First, I read this article that explains the difference between declarations and definitions: http://www.cprogramming.com/declare_vs_define.html

第二,我从以前的研究中得知,在头文件中定义变量和函数是不明智的做法,因为在链接阶段,您可能对同一个名称有多个定义,这将引发错误.

Second, I know from previous research that it is bad practice to define variables and functions in a header file, because during the linking phase you might have multiple definitions for the same name which will throw an error.

但是,为什么这在课堂上没有发生?根据另一个SO答案( 定义与声明之间有什么区别?),则以下为DEFINITION类:

However, how come this doesn't happen for classes? According to another SO answer ( What is the difference between a definition and a declaration? ), the following would be a class DEFINITION:

    class MyClass {
        private:
        public:
    };

如果以上定义在头文件中.然后,大概可以拥有#include该标头的多个.cpp文件.这意味着该类在多个.o文件中编译后被定义了多次,但是似乎不会引起太大的问题...

If the above definition is in a header file. Then , presumably, you can have multiple .cpp files that #include that header. This means the class is defined multiple times after compilation in multiple .o files, but doesn't seem to cause much problems...

另一方面,如果它是在头文件中定义的函数,显然会导致问题……据我所知……mayb?

On the other hand, if it was a function being defined in the header file, it would cause problems apparently...from what I understand... mayb?

那么类定义有什么特别之处?

So what's so special about class definitions?

推荐答案

一个定义规则(3.2,[basic.def.odr])对类和函数的适用方式有所不同:

The one-definition rule (3.2, [basic.def.odr]) applies differently to classes and functions:

1-任何变量,函数,类类型,枚举类型或模板的翻译单元不得包含多个定义.

1 - No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

[...]

4-每个程序都应只包含该程序中奇特使用的每个非内联函数或变量的一个定义.[...]

4 - Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program [...]

因此,尽管(非内联)函数在整个程序中最多可以定义一次(如果调用或以其他方式使用,则只能定义一次),而类的定义次数可以与您拥有翻译单元的次数定义相同(源)文件),但每个翻译单元不得超过一次.

So while (non-inline) functions may be defined at most once in the whole program (and exactly once if they are called or otherwise odr-used), classes may be defined as many times as you have translation units (source files), but no more than once per translation unit.

原因是因为类是类型,所以它们的定义对于在翻译单元之间共享数据是必不可少的.最初,类(C中的struct)没有任何需要链接器支持的数据. C ++引入了虚拟成员函数和虚拟继承,这需要链接器支持 vtable ,但这通常可以通过将vtable附加到成员函数(的定义)来解决.

The reason for this is that since classes are types, their definitions are necessary to be able to share data between translation units. Originally, classes (structs in C) did not have any data requiring linker support; C++ introduces virtual member functions and virtual inheritance, which require linker support for the vtable, but this is usually worked around by attaching the vtable to (the definition of) a member function.

这篇关于在头文件中定义类时,为什么没有多重定义错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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