Why is there no multiple definition error when you define a class in a header file?

你说的曾经没有我的故事 提交于 2019-11-27 18:14:44

问题


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

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.

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:
    };

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

So what's so special about class definitions?


回答1:


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

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

[...]

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.

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.




回答2:


A class definition is just a kind of a blueprint for the objects of that class. It's been the same with struct since the C days. No classes or structures actually exists in the code as such.




回答3:


Your class definition defines the class, but does not define and objects of that class. It's OK to have the class (or structure) defined in multiple files, because you're just defining a type, not a variable of that type. If you just had the definition, no code would be emitted by the compiler.
The compiler actually emits code only after you declare an object (i.e. variable) of this type:

class MyClass myvar;

or:

class MyOtherClass { 
    public: ...
    private: ...
} myvar;         // note the variable name, it instantiates a MyOtherClass

That is what you do NOT want to do in headers because it will cause multiple instances of myvar to be instantiated.



来源:https://stackoverflow.com/questions/14654377/why-is-there-no-multiple-definition-error-when-you-define-a-class-in-a-header-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!