Including Header Files

Started by
10 comments, last by Satharis 11 years ago
For me, I #include anything that I use that is not specifically dependent on an already included header... that was worded weird. Let me give an example:

Header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
 
#include <vector>
 
struct MyStruct
{
    std::vector<int> getStuff();
};
#endif

Now, if I use vector because of Header.hpp, then I don't #include <vector>:
#include "Header.hpp"
 
// I don't #include <vector> because the only time I'm ever using it is because of
// functions/classes defined in Header.hpp (that is, it's because of Header.hpp I'm
// using a vector here at all, so I don't include <vector> because it's Header.hpp's
// responsibility to include its dependencies)
MyStruct s;
std::vector<int> v = s.getStuff();
 
// That is, MyStruct is defined in Header.hpp, and so is its getStuff() method. If I
// use a vector because of MyStruct::getStuff(), then I don't include the vector

However, to continue the example, if I ever use vector in a way that is not associated with Header.hpp, then I #inlcude <vector>
#include "Header.hpp"
#include <vector> // Notice I'm including vector
 
// Here is the same code as before...
MyStruct s;
std::vector<int> v = s.getStuff();
 
// Here, I'm using vector in a way that is not caused by Header.hpp, so I make sure I manually #include <vector>
std::vector<float> other = {1.0};
Note that I also manually include any dependencies if references/pointers are used to non-standard objects (because these things could be forward declared (but you can't (legally) forward declare standard types (except for a few exceptions))). So in the previous example, if getStuff() returned a MyOtherStruct* instead, then I would always include "MyOtherStruct.hpp" (in both code samples) because then Header.hpp can forward declare MyOtherStruct.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement

It's pretty much a stylistic thing, personally I tend to just include to where I don't get any errors for things missing. You of course can be much more proactive about it like making sure you redundantly include headers for things you use in different files even if they're included already in another file.

In the end it really doesn't make a huge difference to the end code as long as it functions. More safety is probably better practice since as a few people have mentioned, other header files can be changed that you include, which may suddenly break some of your code or someone else's.

This topic is closed to new replies.

Advertisement