Re: C++ header file

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<worlman385@xxxxxxxxx> wrote:
Why C++ need header file stores interface (list of function) of a CPP
file?

Why not just have 1 CPP file and include the CPP file instead of
header file?

In Java, there is no header file, each object is .java file.

For each CPP file, there must be a header file map to it.

Is the header file is more analogy to Java's JavaDoc that describe
each member inside a Class?

In addition to the answers of others. Contrary to Java, C++ (and C, as well) may have separate declaration and definition of types and functions. You can declare a type/function in one file, but define (implement) it elsewhere. Usually, a developer puts declarations in header file. Then he implements a function in one of CPP files.

Header file itself cannot be compiled. It is included in other CPP file. Then all #include directives are expanded by preprocessor and the whole thing (it is called "translation unit") is fed to the compiler.

Separation of declaration and definition is very practical thing. It allows you to compile function's definition, which is placed in CPP file, only once. All other CPP files that call this function can be compiled separately. It is sufficient to see only function's declaration in order to successfully compile a code that calls it.

You could place all your code solely in CPP files and then include them one into another. After all, preprocessor doesn't care about file's extension. However, it will produce larger OBJ file with the same redundant code multiplied across many OBJ files. In the past linkers were really stupid, so they just were leaving all the redundant dead code there. So, the ability to separate declaration and definition was important. Today linkers are much smarter, they can remove unused code, so the problem is not that acute anymore.

HTH
Alex

.



Relevant Pages

  • Re: Use of typename
    ... > SDBContainer(); ... > unable to match function definition to an existing declaration ... > It's splitting AddItem out into a .cpp file that's causing the problems - ... > const_iterator of the underlying set seems right, but it won't compile. ...
    (microsoft.public.vc.language)
  • Re: Use of typename
    ... > using namespace std; ... > unable to match function definition to an existing declaration ... > It's splitting AddItem out into a .cpp file that's causing the problems - ... > const_iterator of the underlying set seems right, but it won't compile. ...
    (microsoft.public.vc.language)
  • Re: Singleton class
    ... and therefore every module has a conflicting declaration of this variable. ... This variable should be declared in exactly ONE .cpp file. ... Initially I had put this in to header file, but I changed this to .cpp ...
    (microsoft.public.vc.mfc)
  • Re: forward declaration and #include problem
    ... > This compiles fine, but when I try to use the pointer in CChildDlg.cpp, ... It is always OK to include a header file in a .cpp file (provided you ... forward declaration instead. ...
    (microsoft.public.vc.language)
  • Re: Visual Studio.NET C++ 2005 Express, functions defined in cpp file cause "is not class or namespa
    ... >declared as the following in the header file ... >namespace SDUDataLogger ... >I wanted to implement all the functions in a cpp file as I don't like ... >the function declaration in header file as a prototype. ...
    (microsoft.public.vsnet.general)