Jump to content
  • Advertisement
Sign in to follow this  
Adams555

What is the point of multiple cpp files?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. Currently, I'm using 1 cpp file for each class or group of classes. But the includes are getting a little bit tricky. Considering in ms C++ 6.0 you can jump to any global function, and just search for any others, why would a person use multiple cpp files? I'm considering just cutting and pasting my cpp files together, because I wont have to bother including a bunch of headers all over and stuff. If this is a bad idea, please stop me. Thanks, Adam Here's a few points about my project, just fyi... It's going to be relatively large. (Probably 3k+ lines over time) I'm the only programmer working on it, in my spare time. It will have a client and a server, but they will remain seperate projects.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I have seen 60k line source files.

Share this post


Link to post
Share on other sites
Well...

Things can get pretty tricky. I know (I've experienced it myself).

But it can also be very easy. Like you have your texture (manager) class in one project.

Then you have another project which also uses textures. But you don't want to rewrite your texture manager, so you can copy the cpp and .h file from your previous project, include the .h file and you have a working texture class in your new project at the cost of 1 minute at most (if you have your files organised htat is). :)

Be sure to use the very usefull #ifndef SOME_DEFINE, #define SOME_DEFINE and #endif inclusion guards.
If you have variables (like a Log (singleton) class for logging activities), but you only want 1 instance, then you will have some problems including it into many other files. But you can use extern in every .h/.cpp file (except the log.h file offcourse) and create a new instance in the main file.

So you have in Main.cpp the following:
CLog* Log;

In int Main():
Log = new CLog();

And in every other .h file this:
extern CLog* Log; and you can use Log->printf (or something like that) in those files with out worrying about multiple instances.

I hope this helps.

Share this post


Link to post
Share on other sites
For one, if you're about to recompile after a minor change in your code it's much faster to recompile the 500 line cpp file which you've edited than your whole 3k+ line project.

EDIT:
...and a 3k line project is quite small a project once you get going :)

Share this post


Link to post
Share on other sites
Thanks for your help, but are you saying that it would be a bad idea to combine my source files, or that it would be ok?

Simply, I would only need to include each header once at the beginning of my source file (and in .hs that use others... Or I could combine my headers too, but I dont think thats a good idea, I dunno.)

What my real question is, I guess, is there anything wrong with just using a single cpp file.

Oh, and I highly doubt I will ever reuse any of the classes I am making. And if I do, I can just copy the .f file, and search for the functions in my code and copy them, 5 min at most.


Quote:
For one, if you're about to recompile after a minor change in your code it's much faster to recompile the 500 line cpp file which you've edited than your whole 3k+ line project.

EDIT:
...and a 3k line project is quite small a project once you get going :)


Thank you! That's what I needed. You are quite correct in saying that. I'll stick with my multiple cpp files. The compiles are long enough as it is!

:)

I'll have to increase your rating for that =P

Share this post


Link to post
Share on other sites
how big is the project? if its a small project you wont spend much time on, and you would prefer to, then sure, go ahead and stuff everything in one file. (however, its always good to learn new things, such as how to organize files in your project, so later, when you actually want to do it, you will have experiance doing it).

however, if its even a semi serious project that you will spend a decent amount of time on, then you really should split it into sections. the client to my current project is a little over 70 files and over 15k lines. imagine what that would look like in one big file?

Share this post


Link to post
Share on other sites
Okay, consider yourself stopped. There are many important reasons not to do this.

One which might be enough to convince you to change is that if you make 1 tiny change you'll have to recompile your entire application. If you split it into 10 seperate cpp files for example, then when you make a change in one of them it only has to compile that one, which is going to take one tenth of the time. The realy long compile times would really piss you off before too long.

You are also reducing code re-useability. Are you sure that you think you'll never want to re-use one of the modules in another program? Because extracting the required bits of a class is going to be a real pain later when you could just keep the seperate bits in seperate files for easy re-use later.

If that hasn't satisfied you yet, then [google] for more on this question as there have been many others who have asked it before you.

[edit]
Man I'm a slow typer, that was a lot of cross-posts!
[/edit]

Share this post


Link to post
Share on other sites
It is good to have multiple cpp and h files organized by what they do. Also, when you compile, it only compiles files that have been updated. So if you have everything in 1 cpp file, then it will always have to recompile that file and if it is large then it is a waste of time.

You CAN do it, but it isn't an organized approach and it loses efficiency when compiling. I don't recommend it.

Share this post


Link to post
Share on other sites
Just to answer your question, graveyard, I'm working on a very very simple mmorpg (if thats possible, lol.) I've got the client and server working, and it displays all players connected etc, but no log in yet. So it's pretty small as of yet.

Thanks everyone for helping.

Share this post


Link to post
Share on other sites
hah, simple MMORPG? i'd like to see that [grin].

yes, man, for the love of god if your working on a large project like that you must split the code into sections. im not even sure how you got so far without doing so already [smile].

[Edited by - graveyard filla on January 8, 2005 6:54:26 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!