Public Group

This topic is 3540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Clearly the second version.

The less your headers include, the less you have to recompile if you change something, which will save you a lot of time.

Also, there's less of a danger of cyclic includes.

##### 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 on other sites
Wouldn't this be solved by a quick application of std::string, std::copy, std::stringstream, std::vector and std::shuffle?

##### Share on other sites
Quote:
 Original post by sheep19Hi. 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 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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631426
• Total Posts
3000019
×