When should I not split my code into header and source files?

I know that this can be very subjective, but are there general rules for situations where there is no need to split the code into two files?

For example, is the class extremely small, or if the file just contains some global definitions or static functions? Also, in these cases, should one file be a .cpp file or a .h file?

+5
source share
5 answers

On the technical side, when you need to obey one definition rule , you need to separate declarations from definitions, since you will need to include ads many times in several translation units, but you only need one definition.

In aesthetic terms, the answer can be something like “always” or “systematically”. In any case, you should always have a title for each logical unit of code (for example, one class or one set of functions); and the source file is optional, depending on whether you have everything specific built-in (excluding you from ODR) or if you have a template library.

-, , , . , , , .

+7

.hpp, .cpp :

  • . №1 .cpp ... , .hpp , . , .cpp, .
  • . , (, DLL), , . / . DLL.
  • . .
  • "" .hpp. , , javascript/#/inline ++, .
  • , ​​ inline. ; , , , .

.hpp:

  • , ?
  • / - . .
  • , / , .cpp, . .hpp, , .hpp?

, .cpp .hpp , .

+3

, , - , ?

, .

, :

  • ( )

.cpp .h?

(.h).

0

, , :

, cpp, .

:

  • . .
  • pimpls , , .
  • :
    • , .cpp, .
    • , ,
  • , - , , . :

, , .

, .cpp . .

0

, , , ?

; . , . , , .

,

- .

, , .

?

++. . , , , ".

Also, in these cases, if one file is a .cpp file or a .h file?

The only file should be the header. Rationale: You do not have #includecpp files.

And do not forget that the optimization of the intermodule (aka link-time) is getting better and better.

C ++ compilation time is long and it is very very time consuming to fix it after the fact. I recommend that you move on to the practice of using cpp files before your build times and dependencies explode.

0
source