.cpp
file instead of the .h
file? Are the translation units affected somehow? .h
file and .cpp
file, should I just include it in the .h
file? Will it matter? stdafx.h
), for instance std and third party libraries? How about my own files, should I include them in a stdafx.h
file along the way as I create them? // myClass.h #include <string> // ^-------- should I include it here? -------- class myClass{ myClass(); ~myClass(); int calculation() }; // myClass.cpp #include "myClass.h" #include <string> // ^-------- or maybe here? -------- [..] int myClass::calculation(){ std::string someString = "Hello World"; return someString.length(); } // stdafx.h #include <string.h> // ^--------- or perhaps here, and then include stdafx.h everywhere? -------
Tells the preprocessor to include the contents of a specified file at the point where the directive appears.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program.
GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets.
cpp
-file only. As @Pedro d'Aquino commented, you can reduce the number of includes in a header by using forward declarations whenever possible (basically whenever you only use references or pointers to a given type).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With