• Create Account

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Chad Smith  Members   -  Reputation: 1041

Like
0Likes
Like

Posted 19 April 2013 - 03:41 PM

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

### #2__SKYe  Members   -  Reputation: 945

Like
1Likes
Like

Posted 19 April 2013 - 04:39 PM

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, 19 April 2013 - 04:41 PM.

### #3Dragonsoulj  Crossbones+   -  Reputation: 2008

Like
3Likes
Like

Posted 19 April 2013 - 04:54 PM

Think of it as your header files are pasted directly where you write your #include "header.h". You can include them again, since those particular files are guarded themselves, but you don't have to.

EDIT/Note: If you don't need the #includes in your header, but do need them in your source file, then only put the #includes in your source file.

Edited by Dragonsoulj, 19 April 2013 - 04:55 PM.

### #4SiCrane  Moderators   -  Reputation: 9387

Like
2Likes
Like

Posted 19 April 2013 - 05:02 PM

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.

### #5frob  Moderators   -  Reputation: 18857

Like
4Likes
Like

Posted 19 April 2013 - 05:06 PM

As a stylistic thing, the code:

#pragma once

effectively does the same thing, but it adds protection against many common errors.  Even though it is not part of the language standard it is supported on basically every compiler that matters to games.

One of the most common problems with include guards that every C or C++ programmer will encounter is that the name on the macro must be unique.  It may happen from a copy/paste, it may happen from different libraries that use similar naming conventions, it may happen by shear bad luck, but if you use that method eventually you will be bitten  on a project where a header file will mysteriously not be included because the name is not unique.

There are many other issues as well, such as how the macro may get picked up by other segments of code that are not include guards, that include guards require the file to be opened and parsed (although most compilers have optimized this case), that include guards can corrupt other macros, and so on.

Edited by frob, 19 April 2013 - 05:08 PM.

Check out my personal indie blog at bryanwagstaff.com.

### #6Chad Smith  Members   -  Reputation: 1041

Like
0Likes
Like

Posted 19 April 2013 - 05:19 PM

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.

Edited by Chad Smith, 19 April 2013 - 05:23 PM.

### #7Dragonsoulj  Crossbones+   -  Reputation: 2008

Like
0Likes
Like

Posted 19 April 2013 - 07:41 PM

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.

### #8Chad Smith  Members   -  Reputation: 1041

Like
0Likes
Like

Posted 20 April 2013 - 01:23 AM

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.

### #9Dragonsoulj  Crossbones+   -  Reputation: 2008

Like
0Likes
Like

Posted 20 April 2013 - 08:55 AM

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.

### #10incertia  Crossbones+   -  Reputation: 777

Like
0Likes
Like

Posted 21 April 2013 - 12:49 PM

Personally, if header 'x.h' is used in a file, I #include it, even if I already #included 'x.h' in 'y.h'.
what

### #11Cornstalks  Crossbones+   -  Reputation: 6966

Like
2Likes
Like

Posted 21 April 2013 - 01:09 PM

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, 21 April 2013 - 01:16 PM.

[ 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 ]

### #12Satharis  Members   -  Reputation: 949

Like
0Likes
Like

Posted 21 April 2013 - 09:37 PM

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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS