Followers 0

## 11 posts in this topic

Ok this is such a basic question and I feel sort of dumb for not remembering.  I know how to work with multiple files, include guards and all that.  That's not the issue.  Actually my question isn't really an issue but more of what is correct.

Let's say I have a header file like this

#ifndef FOO_H_
#define FOO_H_
#include <vector>
#include <string>

// the rest of the header file, maybe a Foo Class

#endif


In Foo.cpp when I include Foo.h should I also go ahead and include vector and string or would that just be a waste?

Then lets say in another file I will be using the Foo Class.  So I include Foo.h.  Lets say in that file I also need to use vector and string.  If I just include Foo.h then vector and string should still work just fine?  Though should I also go ahead and include vector and string again, just to make things clear or is this waste just slowing down the compile time?

Is their a "proper" way?  Or could this just be where precompiled header files could come in handy to speed up the compile time process?  I feel dumb having to ask something this basic and something I work with every day though I'm not sure if I was every taught a "proper" way besides just include guards and all that stuff.

Thanks

0

##### Share on other sites

If you include Foo.h in another file, then that other file will also include any files included in Foo.h.

I don't know if there's a proper way or not, but the only thing i can tell you, is that you don't need to include them again. I think it goes a little into the field of coding style.

Edited by __SKYe
1

##### Share on other sites
My general rule is that if I use a class or function in a source file I include the header for the class or function in the source file. Even if it's included already in a different header I include, that header might change later like having an include replaced with a forward declaration.
2

##### Share on other sites

Ok, thanks everyone about when to include the other header files.  I knew that it would still be included that I wasn't sure if it was proper to just go ahead and include them again if I needed them.  I will almost try to remember and keep only including the header for the source file if that's the only place I need it.

frob: I wasn't entirely sure on the compatibility of #pragma once.  I have heard it is becoming more and more supported on a lot of different compilers.  I know it is of course supported in Visual Studio but out of habit I always did include guards even if my code right now has no point of being compatible with other compilers I think I always did it just in case I did switch my code over to something else.  Hearing that it is supported on more and more compilers brings me to want to start using it now to go ahead and get in the habit of using that also.

Also thanks for some issues of include guards.  I knew that the macro has to be unique I guess I never did think of all the different ways that it could end up biting me.

EDIT: Try to rate you up frob for the help but it won't let me right now I guess since I rated the other people up for their quick help and answers.

Use multiple header and source files every day in C++, a feature that seems so simple, and you learn something new every day.  Thanks.

More of a side question and thought: How many people use the .hpp header file extension besides .h?  I know boost uses .hpp and I've heard that some libraries might use .h if it was orgianlly implemented in C and then if they have it implemented in C++ they'll have another file using the .hpp extension.  I guess I just wondered does anyone go ahead and use the .hpp header extension for their every day personal project use?  Just a side thought that popped in my head for some reason out of the blue.

0

##### Share on other sites

Should still be able to rate up frob. Each post can have it's own rate from a user. Maybe just a rate flood limit?

More of a side question and thought: How many people use the .hpp header file extension besides .h? I know boost uses .hpp and I've heard that some libraries might use .h if it was orgianlly implemented in C and then if they have it implemented in C++ they'll have another file using the .hpp extension. I guess I just wondered does anyone go ahead and use the .hpp header extension for their every day personal project use? Just a side thought that popped in my head for some reason out of the blue.

I think this is just a matter of preference, just like using .cc or .cpp for C++ source code.

0

##### Share on other sites

Y ea I figured it was just a rate flood limit think.  When I came back on it let me rate him now.

Yea, I have a feeling it's just a matter of preference, I guess I'm just more so interested in seeing how many people's personal preference is it to use the .hpp for header files.  I just rarely see it except with boost so it's just a random thought I had really.

0

##### Share on other sites

The SFML header includes I use have .hpp in them. It could be handy to use those so they don't accidentally get included in a regular C file/project.

0

##### Share on other sites
Personally, if header 'x.h' is used in a file, I #include it, even if I already #included 'x.h' in 'y.h'.
0

##### Share on other sites
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:

#ifndef 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. Edited by Cornstalks
2

##### Share on other sites

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.

0

## Create an account

Register a new account