Quick Question about Header Includes
Moderator: Coders of Rage
Quick Question about Header Includes
Ok, so let's make this simple and say I have the definition of my cat class in "Cat.h" and then all of its accessor functions/member methods/etc. in "Cat.cpp" then I have my "main.cpp". So with this, where do the includes go, and for what?
Re: Quick Question about Header Includes
you will have cat.h that will look something like this.XianForce wrote:Ok, so let's make this simple and say I have the definition of my cat class in "Cat.h" and then all of its accessor functions/member methods/etc. in "Cat.cpp" then I have my "main.cpp". So with this, where do the includes go, and for what?
Code: Select all
#ifndef _cat_h
#define _cat_h
class cat
{
public:
cat(char* name);
private:
char* name;
};
#endif
Code: Select all
#include "cat.h"
cat::cat(char *name)
{
this->name = name; //im not sure this will work, you might have to allocate mem for this->name first.
}
then lastly main.cpp
Code: Select all
#inlcude <stdio.h>
#include <stdlib.h>
#include "cat.h"
int main(void)
{
cat *meowder = new cat("meowder");
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Quick Question about Header Includes
Yes, thanks that worked, so I have to include the headers in my main.cpp and the .cpp file(s) that are using it?
Re: Quick Question about Header Includes
yup. if you are going to use function or variables or whatever you need to include the .h file that contains that data.XianForce wrote:Yes, thanks that worked, so I have to include the headers in my main.cpp and the .cpp file(s) that are using it?
sometimes i make one h files. something like includes.h, then put all my #include "cat.h" and whatever in there.
then i only inclide the includes.h in all of my files. that way its not as much typing and not as clutered. its especially nice if you say merge 2 files or take one away, you only have to take that definition out of the includes.h and not all your source files.
ps: ALLWAYS use the #ifndef _filename_h and all that jazz
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Quick Question about Header Includes
My IDE automatically does that for me =D. But with your whole thing with the "includes.h" that has all your other header files in it. Wouldn't that take up more memory, and have your program eventually run a lot slower?avansc wrote: ps: ALLWAYS use the #ifndef _filename_h and all that jazz
Re: Quick Question about Header Includes
mmm.. you know im not sure. i dont think so. the #ifndef bla bla besically say has the file been linked to the main program, if not do so.XianForce wrote:My IDE automatically does that for me =D. But with your whole thing with the "includes.h" that has all your other header files in it. Wouldn't that take up more memory, and have your program eventually run a lot slower?avansc wrote: ps: ALLWAYS use the #ifndef _filename_h and all that jazz
so its not like its linking more than it has to. it might make compiling a little bit slower, but nothing major. but should not effect efficiency pr size at runtime.
ps: i might be wrong about this, but i dont think so. so dont qoute me.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Quick Question about Header Includes
How you structure your program before compiling it won't effect it at all in runtime. In theory, putting your program on a single line, or in 10000 different files should execute the same.Wouldn't that take up more memory, and have your program eventually run a lot slower?
The only difference is compile-time. It will take longer to compile, generate the objects, link, etc.
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Re: Quick Question about Header Includes
Alright, great. Time to get back to practice then xDArce wrote:How you structure your program before compiling it won't effect it at all in runtime. In theory, putting your program on a single line, or in 10000 different files should execute the same.Wouldn't that take up more memory, and have your program eventually run a lot slower?
The only difference is compile-time. It will take longer to compile, generate the objects, link, etc.
Re: Quick Question about Header Includes
Haha. sadly, I just ran into a project setup bitchfest involving misuse of headers and source files...Was killing my linker after switching from BloodShed Dev (at school) to MVC++--one covers multiple declaration checks for ya and the other doesn't.
Luckily, marauder bailed me out...But I gotta tell ya, there's no better way to feel like a shitty programmer than to have a computer scientist critique your code. XDDDD
Luckily, marauder bailed me out...But I gotta tell ya, there's no better way to feel like a shitty programmer than to have a computer scientist critique your code. XDDDD
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
Re: Quick Question about Header Includes
Its even worse when you have no idea what the hell their talking about xDArce wrote:Haha. sadly, I just ran into a project setup bitchfest involving misuse of headers and source files...Was killing my linker after switching from BloodShed Dev (at school) to MVC++--one covers multiple declaration checks for ya and the other doesn't.
Luckily, marauder bailed me out...But I gotta tell ya, there's no better way to feel like a shitty programmer than to have a computer scientist critique your code. XDDDD
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Quick Question about Header Includes
By the way... putting multiple includes in one include and including that single file will increase your compile time. If the include guards (#ifndef .. #define .. #endif) are inside the file, then the compiler still has to open the file to see the guards. This happens for every .cpp that it's included in (as every .cpp file is compiled to a .obj file and then linked together). In short, the more effective #includes you have, the longer your compilation takes.
Scenario 1:
scen11.cpp
scen12.cpp
Scenario 2:
scen21.cpp
scen22.cpp
all.h
Scenario 1 compiles faster than scenario 2, because in scenario 2 when main.cpp is being compiled, the compiler has to open all.h and c.h, which is two more files than then first setup had to open.
Additionally, in scenario 2, if you change something in c.h, then all.h changes (since c.h is pasted by the linker into all.h), which means that scen21 changes (since all.h is then pasted into scen21.cpp), which means that scen21.cpp has to be recompiled and so does scen22.cpp.
In Scenario 1, if you change something in c.h, scen11.cpp is not changed and is not recompiled, although scen21.cpp still has to be. So in scenario 2, not only does the compiler have to open more files, it also has to recompile .cpp files more often than scenario 1. C/++ compilation is too slow in the first place to code this way :P
If you want a blatant demonstration, make a for loop that terminates when i = 100 or something. Then add some file i/o inside that loop and witness the huge speed decrease, due to accessing the hard drive. Now make i = 50 and of course it'll run faster since you'll use the hard drive less. Same thing: more includes per .cpp/.obj = more hard drive use = slower compilation time.
Scenario 1:
scen11.cpp
Code: Select all
#include "a.h"
#include "b.h"
...
Code: Select all
#include "c.h"
scen21.cpp
Code: Select all
#include "all.h"
Code: Select all
#include "c.h
Code: Select all
#include "a.h"
#include "b.h"
#include "c.h"
Additionally, in scenario 2, if you change something in c.h, then all.h changes (since c.h is pasted by the linker into all.h), which means that scen21 changes (since all.h is then pasted into scen21.cpp), which means that scen21.cpp has to be recompiled and so does scen22.cpp.
In Scenario 1, if you change something in c.h, scen11.cpp is not changed and is not recompiled, although scen21.cpp still has to be. So in scenario 2, not only does the compiler have to open more files, it also has to recompile .cpp files more often than scenario 1. C/++ compilation is too slow in the first place to code this way :P
If you want a blatant demonstration, make a for loop that terminates when i = 100 or something. Then add some file i/o inside that loop and witness the huge speed decrease, due to accessing the hard drive. Now make i = 50 and of course it'll run faster since you'll use the hard drive less. Same thing: more includes per .cpp/.obj = more hard drive use = slower compilation time.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Quick Question about Header Includes
Wait so it can produce a slower program? and not just increase compile time?MauraderIIC wrote: Same thing: more includes per .cpp/.obj = more hard drive use = slower runtime
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Quick Question about Header Includes
Just slower compile time I fixed that sentence in my OP, sorry.
Click here to see the hidden message (It might contain spoilers)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.