Placement of methods in .h and .cpp files

I saw the code for the class placed in separate C ++, and the method definitions are placed in the header file. My first OOP experience was with Java, in which all methods are placed in a class file, and I really prefer that.

Does all my methods put in the header file the assembly code generated by the compiler, or not?

And if so, is it harmful to performance to put all the class code in its header file?

+3
source share
3 answers

There are several good reasons for splitting the header / implementation and a separate compilation:
1. It may be a work requirement - for example, you are supplying a binary library + header to someone, or your colleagues are too conservative to accept anything else.
2. It is still required for the development of very large projects (say> 10 million Sources), because restoring the entire application after each modification will become painful. (But it should still be good to compile something like jpeglib or zlib as one module)
3. There is an opinion that it is easier to use header files as a reference to look, etc. (But as a rule, it’s better to write the correct documentation, unlike the headers, errors in the documentation less affect your program)

In addition, there are many more reasons not to use it anymore:
1. You would like to avoid code duplication.
2. Class methods do not need forward declarations
3. Templates can be declared only in headers
4. Cases when you do not need a functional insert are actually quite rare, i.e. multiple calls to large functions in a narrow loop, but there are noinline attibutes and PGO for this. Otherwise, embedding improves speed. As for the bloat code, most libraries are already huge.
5. In general, programs compiled as a single source are faster and smaller because the compiler can do a better job.
6. Without headers, the source will often be about half as small, and the compiler will be able to correctly check the syntax, so you cannot accidentally associate the prototype of the "C" cdecl function with a variable as an implementation. Also, it would be more portable, because different linkers have different ideas about matching names.
7. Its strange but dynamic allocation is often used only because of the header style - dependencies can be resolved automatically by defining all the details within the same class, but people prefer to use pointers to declarations with a partial class instead (and then a memory leak) .

Now a few bonus points for individual object modules:
4. PGO statistics in gcc are generated per object module, which, apparently, is the only way to "test" several different modes of operation with one executable file.
5. It can be compiled for different modules with different compiler options to optimize speed. There are some extensions for the compiler for this, but they are not very reliable.
6. Sometimes the compiler can do something strange for another part of the code when you change something, but usually it cannot be distributed outside the object module.

+2
source

, ++- , . (, ".cpp", ".cc" ..), . , , , , - ( ) , , (, - " " - ). . jalf .

, , . inline - - inline , , ​​ . - .

, , , . , , . .

+6

, , , , , , ( ).

, , .

+1

Source: https://habr.com/ru/post/1779121/


All Articles