C++ header file [duplicate]
Possible Duplicate:
In C++ why have header files and cpp files?
Why in C++ is there a .h and .cpp and not only one file like c# and Java ?
For historical reasons. Specifically, for compatibility with C. That language was originally designed to run on (for 70s standards) low-end machines; header files were (and often still are) substituted inline by a separate program, to keep the memory use of the compiler down. It still helps to keep libraries small.
Because c# and java do not require forward declarations. C++ requires the forward declarations.
C++ has a pre-processor that it inherited from C. The pre-processor has many interesting features, but one of the things that it is used for is to structure the code into headers and source files.
Structuring the code into headers and source files allows you to determine which parts of the code are visible to other source files (i.e. the parts you put in the header) and which ones aren't.
It also means that you don't need any special tools to know which classes are available to you, as you would if you had import
in stead of #include
: you have part of the source code to work with in stead (which you can read with any text editor).
The advantages notwithstanding, the pre-processor is really a legacy from C++'s parent, C, and has survived the evolution of C++ almost entirely in-tact.
精彩评论