Header files, .cpp files, and #include

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

Recommended Posts

I know #include is great for putting classes in different files. I want to break up a file into multiple ones, to make it easier to read. I have a big main file, main.cpp, with a lot of methods in it. I want to put these in a different file, helper.cpp. What is really confusing me is how to put everything together with #include. #include just puts the text of the file directly in the spot, right? Then how come if main.cpp had #include <a.h>, I have to include a.h in my helper.cpp file too? Shouldn't helper.cpp be technically already in main.cpp, and thus "see" #include <a.h>? And aren't I supposed to watch out for including the same file twice(which can be prevented by those #ifndef header guard things)? Or am I not supposed to do this at all? Also, what is the difference between header files and .cpp files? I know class declaration stuff is in headers, and it's implemented in .cpp, but I don't know why. What is the technical difference, and can I do what I wanted to before in a header file instead? Thanks.

Share on other sites
including a .h file into a .cpp file just tells the compiler that the information that is need is here. It does not globally include that file into every file.

What you said is true, .h files are used to declare and cpp to initialize, you can initialize code in .h files but that just makes them longer and defeats the purpose of them. you can spread code out around different cpp files and include the same header in all of the cpp files.

Share on other sites
It' quite safe (IMHO) to say that you define classes and structs in .h and implement them in cpp files. You cannot (or better should not) define a type in a cpp file and use it in other cpp files, this is why you use headers and include them in the source (maybe other headers)...

When your cpp source needs some data contained in a header file, just include it. If it uses guards, you don't need to care about it be included multiple times (directly and indirectly). In most cases, when you use a class the compiler must know the methods contained by that class (thus the need to include the header) but the actual implementation is not needed because it will be available after the linking step.

Share on other sites
Each translation unit (basically, a .cpp file) will be compiled seperately.

Share on other sites
Oh, so including it again is just for the compiler's convenience (and Intellisense's!). Good article.

So for my other file, since it's not a class, but just a bunch of methods, do I need a header file for it? The article says it's a good idea to have a header for each source file, but if it's just a bunch of functions, do I need one? What would I put in it? It's only going to be included in my main.cpp file.

Also, what's so bad about global variables and #define? They would help out my program a bit. What if I needed to make like a physics constant, which all physical movements are based on (and would need some fine tuning later on). Should I have a global.cpp file where I #define PHYSICS_CONSTANT .001, or should I make an external constant variable?

Share on other sites
Quote:
 Oh, so including it again is just for the compiler's convenience (and Intellisense's!). Good article.
No. You don't include header files if you don't need them. Stop, and think. What is #include? It's a copy-paste directive. That's it. An automatic copy paster. why would you dump a header file into a source file? Because the header file contains declarations for things the source file uses.

You don't need to use anything the header file declares? Don't include the header file. Doing so needlessly increases compilation time.

Quote:
 So for my other file, since it's not a class, but just a bunch of methods, do I need a header file for it?
So, let’s see. helper.cpp contains a bunch of methods, and main.cpp uses those methods? Well, since you are using those methods in main.cpp, they need to be declared. Might as well put those declarations in a header file, and include the header, right?

Quote:
 What would I put in it?
Declarations. You know...

//This is a declaration. No code, just the function signature.//A declaration would go in the header file. //Which then gets auto copy paste wherever necessary.int addTwo(int, int); // Now the definition, i.e. the actual codeint addTwo(int firstN, int secondN){    ... // Code}

Quote:
Inefficient, artifacts of bad design, unmanageable in large code bases.

If you have variables that every function and bit of code uses, then it means you have spaghetti code. It’s tightly coupled code, so interdependent that changes will be impossible. You want modular, low coupling code.

Quote:
 and #define
Because a #define is a sort of copy paste as well. There are certain uses for them (e.g. header guards) but if you need to define a constant: then just make a variable a const. The benefit is that you are actually using the language, and get all the benefits of the language with constants. With defines, you get nothing but copy paste systems.

Share on other sites
I see. For the first part, I meant even though the header file is already included in another file, I have to include it again so that the compiler knows what to look for, even though it's not going in the actual output file twice (if I use inclusion guards).

And yeah, I forgot about the prototypes for the functions. That should indeed be in the header.

So as an alternate for global variables, would the best alternative be to pass the variables as a parameter to the function that needs them?

Thanks!

• 48
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631380
• Total Posts
2999672
×