[C++] Class Question

Junior Spellweaver
Joined
Mar 25, 2007
Messages
126
Reaction score
0
Hi. i have learned how to write classes. My question is, how can i write my Class on another .cpp file and include its functions in the main.cpp?
 
Or else you'll get errors when yourfile.h is attempted to be included several times.

Yeah, its quite standard to include the define, but when do you get header files to be included multiple times? I just include everything I need in the top of main.cpp (or main.h if I have one).
 
Yeah, its quite standard to include the define, but when do you get header files to be included multiple times? I just include everything I need in the top of main.cpp (or main.h if I have one).
Well for one, when you have a class in a separate file? I admit I'm not 100% confident about how they get processed, but it's good practice to say the very least.
 
Yeah, its quite standard to include the define, but when do you get header files to be included multiple times? I just include everything I need in the top of main.cpp (or main.h if I have one).

The reason you want to put the check is because, let's say several files include your header file. If there is no check, then if those other files are ever used together, your header file will be included multiple times.

For example, let's say <fstream.h> requires <stdio.h> (I dunno if it does, I'm just making it up), so it includes it, and let's say <iostream.h> also uses <stdio.h>, so it includes it. Now let's say you write a program that uses both <iostream.h> and <fstream.h>. If <stdio.h> didn't have the check, you would end up including it 2 times.
 
The reason you want to put the check is because, let's say several files include your header file. If there is no check, then if those other files are ever used together, your header file will be included multiple times.

For example, let's say <fstream.h> requires <stdio.h> (I dunno if it does, I'm just making it up), so it includes it, and let's say <iostream.h> also uses <stdio.h>, so it includes it. Now let's say you write a program that uses both <iostream.h> and <fstream.h>. If <stdio.h> didn't have the check, you would end up including it 2 times.

Lol, I can fix that fairly easy:

#include <stdio.h>
#include <iostream.h>
#include <fstream.h>

And don't include headers in the header itself.
 
Well, the problem is that <iostream> won't compile on it's own then if it uses functions from <stdio> and didn't include it, and same goes for <fstream>.

But even ignoring that fact for a moment, if you want to use any file, you shouldn't be required to know every single file that must be included for the file you actually want to work. And from a practical, as well as abstracted, point of view, it's not your job to know that.

Let's say you want to use a completed 3d game engine written by someone else, that uses a lot of includes. The writers of the game engine will put all the includes necessary in their code, and for simplicity, all you would have to do is include the game engine files, instead of dig into their code to find all all the files they used. Let's say you later decided you didn't want the audio portion of their engine, you'd then need to go through all their code again and figure out which functions they use for the sound, make sure other parts of their code don't use those functions too, and only then would you know what is safe to have removed from your includes. As you can see, it becomes very impractical.

Another consideration is that you wrote a file that you didn't intend to be used elsewhere, so you left the includes in. Then someone else wanted to use your code, and included your file. Point is, it is confusing and an enormous waste of time to remove includes from all other files and place them only in your main file or the one you're working on, and then have to remove those includes and replace them in another file when you try to modify that other file. Then when you want to modify the library code, you'll need to place back the includes in the library and make sure all the other libraries don't include them, etc etc. Much easier with a simple define/ifndef.
 
Back