Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

So what's so special about class definitions?

like image 785
codecitrus Avatar asked Feb 01 '13 20:02

codecitrus


People also ask

Can you define a class in a header file?

Class definitions can be put in header files in order to facilitate reuse in multiple files or multiple projects. Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a .

Can you put definitions in header files?

Because a header file might potentially be included by multiple files, it cannot contain definitions that might produce multiple definitions of the same name. The following are not allowed, or are considered very bad practice: built-in type definitions at namespace or global scope. non-inline function definitions.

What is the error of multiple definition of main in C?

Multiple definition of main means you have written main function more than once in your program which is not correct according to C language specifications.

Can a header file have multiple classes C++?

If A and B were normal classes, and A and B's methods were in . CPP files, there would be no problem: You would use a forward declaration, have a header for each class definitions, then each CPP would include both HPP. But as you have templates, you actually have to reproduce that patterns above, but with headers only.


2 Answers

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.

like image 56
ecatmur Avatar answered Sep 29 '22 00:09

ecatmur


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.

like image 42
Some programmer dude Avatar answered Sep 29 '22 01:09

Some programmer dude