Does it matter if I add the #include directive to my cpp file or to the header file with the included content?

My C ++ program uses a separate header file (let him call it myHeader.h) and therefore includes it ( #include "myHeader.h"). In my program, I need to use a different header file (let him name it another.h). Does it matter whether I put the directive #include "another.h"in a cpp file or in myHeader.h?

+3
source share
6 answers

If it is not used in the file .h, then there will be no difference in success / failure of compilation.

However, it is recommended that you include include for header files that you only need to implement in files .cppfor the following reasons:

  • - , .
  • A.h B.h , B.h include A.h. .
  • , ( , , ).
+14

cpp, cpp.

, , . , , , .

+7

: "". .h, . .

+5

, .., .

, .

, , include, .

, , cpp .

+1

. cpp . .

0

- , h , , h , - ++, , .

. " ++" ), , ++-. 1996 , , .

0

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


All Articles