Page 1 of 1
Is it even necessary?
Posted: Wed Apr 27, 2011 10:43 pm
by MadPumpkin
Is it even necessary to have .cpp AND .h files in my project? Because for my entire project I've been using once main .cpp and all headers declare and define functions in the class definition out of pure laziness, when I realized that it worked fine either way. Should I not do it this way? It doesn't cause any errors. Is there any reason why I need to use .cpp's?
Re: Is it even necessary?
Posted: Wed Apr 27, 2011 11:38 pm
by XianForce
From my understanding, it does have an impact on compile time... The inclusion of a header takes place before the code is actually compiled and (from what I understand) more or less "copies and pastes" the header into whatever file. So if you define your functions in there, you'll have those functions being compiled multiple times... But that does bring up another probable issue... I've never heard anything of it, or seen it myself, but I would assume that it would create an issue with multiple definitions of functions?
Re: Is it even necessary?
Posted: Wed Apr 27, 2011 11:39 pm
by gamenovice
My guess is that .cpp is the only file actually compiled into machine code to run, nothing else, and that's why you need it. .h is for organization purposes, but you are rite since they are not essentially needed to create the program(this is just my guess so correct me if i'm wrong)
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 12:28 am
by like80ninjas
I'm no guru or anything, but It improves organization and readable by a huge amount. I don't think there are any technical advantages, but I do know that it is a standard, and standards are there for a reason:)
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 1:25 am
by X Abstract X
XianForce wrote:From my understanding, it does have an impact on compile time... The inclusion of a header takes place before the code is actually compiled and (from what I understand) more or less "copies and pastes" the header into whatever file.
Mainly this.
Also, it's easier to glance at a header file to get an overview of a class when you don't have to dig through a huge header file filled with function definitions.
Just a guess but perhaps the compiler will inline a lot more of your functions if your defining them all in the header, resulting in a much larger executable file size.
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 2:39 pm
by Boogy
Header files are a way to hide the implementation aka abstraction. It also allows standards to be followed between cpp files.
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 2:56 pm
by N64vSNES
Back in the days when I used C I probably never used .cpp files either. I'm not sure if it affects compile time but I remember running into a LOT of redefinition errors but maybe that's because I used Dev-C++ (insanely buggy)
When you get used to splitting it into other files you'll probably find the benefits out weigh the down falls.
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 3:05 pm
by MadPumpkin
Alright thanks guys, I'm actually used to splitting it. I'm more used to splitting them in fact. But after doing so much Visual C++ form programming for my editor, I got lazy and didn't feel like it till now. I have gotten redefinition errors but only after switching to SDL instead of SFML (SFML doesn't support mouse capture). Mostly Executable file size and compile time are what I'm worried about so thanks, question answered.
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 3:22 pm
by dandymcgee
The only time I ever do this is when creating a class template. Aside from this there's no reason other than laziness not to separate declaration from definition. Even in the case of templates, some people choose to go out of their way to achieve similar separation.
Re: Is it even necessary?
Posted: Thu Apr 28, 2011 3:48 pm
by Ginto8
Here's the real issue with a ton of .h's and one .cpp: you're compiling EVERYTHING all over again each time. Say you have a project that's a few thousand lines of code. It would be very inneficient to compile everything again each time, when only a couple of things have changed. So why shouldn't you? Because separating it into .h/.cpp pairs is MUCH better for the compilation process.
Re: Is it even necessary?
Posted: Mon May 02, 2011 6:56 pm
by Falco Girgis
Ginto8 wrote:Here's the real issue with a ton of .h's and one .cpp: you're compiling EVERYTHING all over again each time. Say you have a project that's a few thousand lines of code. It would be very inneficient to compile everything again each time, when only a couple of things have changed. So why shouldn't you? Because separating it into .h/.cpp pairs is MUCH better for the compilation process.
^ What he said.
This is a HUGE deal. That's a terrible, terrible design practice. You're also telling the compiler to automatically inline your class member functions. I would hope to god that it's not dumb enough to listen to you, otherwise your compiled code is going to have some SERIOUS bloat issues.
That literally won't even compile with straight C, unless you explicitly tell the compiler to inline your functions. You'll get errors that the symbol is redefined in every .c file you've included your header in.
The ONLY time that you should EVER not separate the interface of your class from its implementation is when you are inlining the class. This ain't C# or Java, you don't pull that kind of shit with C/++.
Re: Is it even necessary?
Posted: Tue May 03, 2011 7:48 am
by GroundUpEngine
X Abstract X wrote:
Also, it's easier to glance at a header file to get an overview of a class when you don't have to dig through a huge header file filled with function definitions.
Boogy wrote:Header files are a way to hide the implementation aka abstraction. It also allows standards to be followed between cpp files.
Absolutely!