Sign in to follow this  
sheep19

including headers files question

Recommended Posts

Hello. Which is better: THIS: s.h
#ifndef S_H
#define S_H

#include <iostream>
#include <vector>
#include <...>

void sayHi();

#endif



s.cpp
#include "s.h"

sayHi()
{
std::cout << "Hi!\n";
}



OR s.h
#ifndef S_H
#define S_H

void sayHi();

#endif



s.cpp
#include <iostream>
#include <vector>
#include <...>

#include "s.h"

sayHi()
{
std::cout << "Hi!\n";
}



Which is better and why? [Edited by - sheep19 on October 14, 2008 10:55:38 AM]

Share this post


Link to post
Share on other sites
None - because you're not using any std::vector in your .cpp file, so including <vector> makes no sense. ;)

Now, for a more serious answer, I'd choose the latter, because it's only including headers where they are actually used. In the former case, any other .cpp file that includes your header also gets the iostream and vector headers included. But it doesn't need them - it only needs to know about your SayHi() function.

Share this post


Link to post
Share on other sites
B)

You should, as a matter of style really, always explictly include files. Your headers should be as highly cohesive and as low coupling as possible. The header should compile on its own, and should only include anything necessary for it to compile on its own.

Share this post


Link to post
Share on other sites
As a programmer, I have my expectations. If you have a module Foo in a header file Foo.h, then I expect to write a simple #include "Foo.h" and be able to work with the module Foo.

Sure, if Foo interacts with Bar, I can accept that I have to also #include "Bar.h" to use Bar. But I shouldn't have to include it if I don't use Bar in my own code.

The basic rule is: Your file should not cause a compile error if it's included alone. As long as you respect this rule, make sure to put as few headers as possible in any file.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Hi. I've got another problem, I've updated the original post.


Please don't do this in the future, it makes the thread much harder to follow (similarly, don't mark threads as "solved"). It's perfectly acceptable to continue discussion later in the thread, or if you feel you need more visibility on the question, creating a new thread entirely.

Share this post


Link to post
Share on other sites
Editing your original post isn't a great idea in my opinion. It breaks the "flow" of the forum. For example, I saw that this topic had received some more replies since I had last seen it. The first thing I do is scroll down to roughly where I had been. A few posts later, I am instructed to go back to the top?

I think either of these is better

* Start a new topic (makes sense in this situation, the two questions are unrelated).

* Just post your question as a reply towards the end of the other thread.

Finally, using [solved] (or similar) tends to be frowned on here.

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