C++ Style Headers

Started by
3 comments, last by Andrew Russell 18 years, 10 months ago
Hi All, I understand that new C++ style headers now ommit the '.h' as they can represent an abstraction of some kind, not necessarily a physical file. That being said, is there then a way I can define my OWN class outside of a normal '.h' file (ideally in the .cpp source file) and have it abstracted into my own C++ style header so I can have only one source file? At the moment I have my class definition in a '.h' file and the functions in the .cpp file with an include of the .h file at the top. Thanks
Advertisement
You can put class definitions in a .cpp file; but if there are other .cpp files in your project which need that class definition, you will have to #include that first .cpp file.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Quote:Original post by Verg
You can put class definitions in a .cpp file; but if there are other .cpp files in your project which need that class definition, you will have to #include that first .cpp file.


So I can't use the C++ header format of...

#include <myclass>

I must always use

#include "myclass.h"

or

#include "myclass.cpp"

Is it preferred style now to include the cpp file rather than a header file?

Thanks
You can #include anything, the extension is largely irrelevent. Whether it compiles or not is another matter.

#including a cpp file is a generally a bad move and certainly isn't preferred style, because these are understood by the compiler as files that should be compiled and linked. #including these will likely give you a ton of linker errors due to clashing symbols.

In the case of the stl and many of the 'new style' C++ io headers, the files have no extension, so you drop the .h when you #include them.
OK, while the Standard C++ Library headers have no extention, you can use whatever extention you like for your own header files. ".h" or ".hpp" are highly recomended. Some people use ".H" or ".hh" or other crazy things too. No extention for your own headers is generally a bad idea.

When declaring classes, unless it is only going to be used in that one C++ file, it should generally go into a header file. Having a class defined in a C++ file is rare, so you're probably safest putting it into a header.


When you include header files for your project, it generally should look like this:

#include "mything.h"

The < > style brackets are for things in the compiler's include path (rather than relitive to the file the compiler is compiling) - this is basically, all your system and library headers and such.


Also, don't forget include guards on the header. If you don't know what these are, basically put this at the top:

#ifndef MYTHING_H_INCLUDED
#define MYTHING_H_INCLUDED

And this at the bottom:

#endif

Change "MYTHING_H" to whatever the name of your header file is. This prevents the file from being included twice (functionally).

This topic is closed to new replies.

Advertisement