Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   a .cpp file always include the corresponding header file first (http://www.go4expert.com/forums/cpp-file-include-corresponding-header-t8260/)

princemaozh 14Jan2008 20:28

a .cpp file always include the corresponding header file first
 
In many articles, there is such a coding guideline:

>> On a .cpp file always include the corresponding header file first, like this:
Code:

// This is Foo.cpp
 
#include "Foo.h" 
#include "tao/Bar.h"
#include "ace/Baz.h"
 
// Here comes the Foo.cpp code....

>> In this way we are sure that the header file is self-contained and can be safely included from some place else.

Why it can make sure the header file is self-contained by including the header file first in the corresponding implementation file? Can anybody explain it a little bit more?

Salem 15Jan2008 03:01

Re: a .cpp file always include the corresponding header file first
 
By making sure that foo.h includes gromit.h and widget.h automatically, you save having to explain to all users of foo.h the need to include the other two files, and save having to remind them should they accidentally forget.

If at some future time, foo.h removed gromit.h and adds wallace.h, then all the users of foo.h don't have to go round changing many thousands of instances of including foo.h to fix the pre-requisite headers.

princemaozh 15Jan2008 07:32

Re: a .cpp file always include the corresponding header file first
 
Salem, thanks for your answer. But it seems you explained the advantage of making the header file "self-contained".

My question is:
Why "a .cpp file always include the corresponding header file first (see example above)" is able to check whether the header file is self-contained?
What will happen if I include other header files before the corresponding header file of that .cpp file?

Salem 15Jan2008 15:21

Re: a .cpp file always include the corresponding header file first
 
By being included first is how it checks.

shabbir 15Jan2008 17:20

Re: a .cpp file always include the corresponding header file first
 
Quote:

Originally Posted by princemaozh
What will happen if I include other header files before the corresponding header file of that .cpp file?

No Difference. It does not depend on the order of file inclusion but need to include before you first need to use them.

princemaozh 23Jan2008 19:58

Re: a .cpp file always include the corresponding header file first
 
Quote:

By being included first is how it checks.
Salem, do you mean if the cpp file does not include the corresponding header file in the first line (e.g. include other .h files, then its corresponding header file), it has no chance to check whether the corresponding header file is self-contained?

Quote:

No Difference. It does not depend on the order of file inclusion but need to include before you first need to use them.
Shabbir, seems your answer conflicts with Salem's.

I get a little confused.

shabbir 23Jan2008 21:26

Re: a .cpp file always include the corresponding header file first
 
I could not get the meaning of the post you are quoting though.

Salem 24Jan2008 00:55

Re: a .cpp file always include the corresponding header file first
 
Well if you had this
Code:

#include "other.h"
#include "myHeader.h"

Then there may be the possibility that myHeader.h depends on other.h in some way.

But by having this
Code:

#include "myHeader.h"
#include "other.h"

There is no possibility of a hidden dependency.
Either myHeader.h doesn't depend on other.h, or it does depend on other.h and it (myHeader.h) includes other.h as well.

So by including it first, you guarantee that it has no hidden dependencies (otherwise it would fail to compile). This gives confidence that anyone else who needs myHeader.h can include it without having to worry about dependencies.

shabbir 24Jan2008 07:51

Re: a .cpp file always include the corresponding header file first
 
So there is no conflict now because what I said was syntactically correct and what Salem is saying is what you should be doing for better understanding of code.

asadullah.ansari 24Jan2008 11:04

Re: a .cpp file always include the corresponding header file first
 
use
#pragma once

In case of to avoid too many times including the same header file


All times are GMT +5.5. The time now is 06:15.