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

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

问题描述

我不确定我是否正确地问了这个问题,但让我解释一下.

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...

另一方面,如果它是在头文件中定义的函数,它显然会导致问题...据我了解...也许?

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

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

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 - 每个程序都应包含该程序中 odr 使用的每个非内联函数或变量的准确定义 [...]

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 中的 structs)没有任何需要链接器支持的数据;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天全站免登陆