Sign in to follow this  
Dko

A question about .h and cpp files.

Recommended Posts

Hemm... I was just wondering. Was looking thru two books of mine and there source code. One is a bit older and that I think has a newer version out. The way they have .h files and there corrisponding .cpp files is a bit different. One had #pragma once and included .h files (Like iostream) with the .h file. While the other had the whole #ifndef, #define setup and all includes where with the .cpp file. The second one is the more accepted practice right?

Share this post


Link to post
Share on other sites
If I'm not totally mislead here, #pragma once was introduced by Microsoft. GCC also declared support for it in the meantime, but it's not a part of the C++ standard.

The catch is, #ifndef .. #endif include guards require the compiler to actually open the file and run it through the preprocessor, while #pragma once is supposed to make the compiler insert the header's filename in its internal already-included list so it can just skip the entire file if it sees it included somewhere else. In larger projects with many small headers, this can be a substantial improvement in compiling speed.

Some OS projects actually use both, so it works on a plain standards conforming compiler and is optimized in compilers with #pragma once support.

-Markus-

Share this post


Link to post
Share on other sites
Here is an intresting read on this matter as well. If I am not mistaken either, you can achieve the functionality of #pragma once with #ifndefs if you structure them right. Basically you will be using them in the .h and .cpp files. You will use them normally in the .h files but in the .cpp files, you use include gaurds on the lines that include files.

Here's a quick example:

//Main.h
#ifndef _MAIN_
#define _MAIN_
...
#endif

//Main.cpp
#ifndef _MAIN_
#include "Main.h"
#endif
...


You basically will follow that structure for the respective files. I do not know how well this works for all projects though. I think I had tried in on a rather large fun project I had and a few dependices were mixed up, so I do not know if it was me or the way thie method works. I can't remember the exact source I read this at, but it had to do with gaming.

- Drew

Share this post


Link to post
Share on other sites
ohh one thing I forgot to ask explicidly about is where is it best to have other includes. In the .h file or the .cpp file?

Ex 1. includes in the .h file
//.h
#include <iostream>
code...

//.cpp
#include ".h"
code...

Ex 2. all include in the .cpp file
//.h
code...


//.cpp
#include <iostream>
#include ".h"
code...

Share this post


Link to post
Share on other sites
That is a question that depends on your project. I've heard both sides of the argument that you are not supposed to include header files inside header files and you are. I can't really comment which one is better. What I do is that if I need a header file in another header file, I do that, if not, then I don't. I'm sure you will get a better response taking either side. I'm more netural on this topic. Really though, there is no 'best' way that fits all [smile].

- Drew

Share this post


Link to post
Share on other sites
In header files, do include any headers that are required just for the stuff in the header itself. (If you put those includes in the cpp, you would have to remember to include them before the header itself, and could run into a problem with that due to circular dependency. This is exactly what the include guards are there for - to allow you to do the inclusion you need like this.) For other headers, it's a judgement call, but you will probably want to include most of them in the .cpp.

Share this post


Link to post
Share on other sites
Here's a good trick to avoid headers being dependent on other header they do not include themselfes: In any .cpp with has an associated header, always include this header first and before all other. When the header isn't stand-alone and has hidden dependencies, the compiler will bail.

-Markus-

Share this post


Link to post
Share on other sites
Including header files in other header files is required sometimes.

Image you have a class B derived from another class A. Then to write down something like:

class B : public A { ... };

you need to know the definition of A. So include A.h in B.h in this case.

When you just need a pointer to some type like in:

class A;
int func(A* a);

it is not required to include the full header file, but just declare A (with 'class A').

This also avoids circular dependencies of header files, which cannot work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this