Is it even necessary?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Is it even necessary?

Post 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?
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it even necessary?

Post 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?
User avatar
gamenovice
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Fri Nov 12, 2010 7:49 pm
Current Project: wii u dev, sdl, and some unity 3D
Favorite Gaming Platforms: PC, GAMEBOY, GAMECUBE, WII, 3DS,PS2
Programming Language of Choice: C#,C++,Java
Location: Tampa,FL
Contact:

Re: Is it even necessary?

Post 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)
without code, we wouldnt have life as we know it...
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: Is it even necessary?

Post 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:)
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Is it even necessary?

Post 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.
User avatar
Boogy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 27
Joined: Sun Mar 06, 2011 3:59 pm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Breda, The Netherlands
Contact:

Re: Is it even necessary?

Post by Boogy »

Header files are a way to hide the implementation aka abstraction. It also allows standards to be followed between cpp files.
Student @ IGAD
Image
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Is it even necessary?

Post 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.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Is it even necessary?

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Is it even necessary?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Is it even necessary?

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Is it even necessary?

Post 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/++. ;)
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Is it even necessary?

Post 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!
Post Reply