Code cpp and .h

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

Code cpp and .h

Post by screwgh »

Hi,

I just rearanged my code into a bunch of .h files and everything works fine. But the question that sticks to my mind is why does some projects also have more .cpp file almost every .h has a .cpp file why is that?

Do I need to do this also for my project and why is that? Also how do I do it? I made some programs that had .cpp files for every .h file. But that is from the start. I don`t get it working if I start now with all that.

Is it wrong to just have the main.cpp and the rest .h files? Even if it works great?

Maybe somebody has a good explanation for me. Thanks in advance.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

If it works then it works... it's not hugely wrong i guess but it's not ideal.

Generally you put minimal stuff into the header files so that you can use classes, functions and variables that are necessary elsewhere and you put the bulk of function and class implementations in the .cpp files.

This is because when you do a #include on a .h file at compile time it copies the entire .h file into that point of the file you've put the #include at. So if your .h files are huge this is going to take a long time to compile. I guess in terms of the resulting application it doesn't matter.. though i'm sure you could run into some nasty compilation issues doing things this way.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

There's several reasons.

First, the C++ idiom suggests keeping the class declaration (what it does) separate from the definition (how it does it). Thus the .h with the class declaration should hide the implementation details.

Second, if you define a method inside the class declaration, you're implicitly inlining it. You may or may not want that to happen. The general idiom is to declare inline methods explicitly in the .h, then define them elsewhere.

Now, both of those make a lot of sense for a library, particularly when you are declaring pure virtual interfaces (like Irrlicht's IFoo interfaces) which really are designed to hide the implementation. However, when you're developing a user app, particularly a 3D app where presumably you want most everything to be inlined anyway, the advantages are reduced.

There's a third reason, that mutual dependencies can crop up if you try to define everything completely in headers.

However, there's a workaround presented in Java style classes in C++ which allows entire implementations in headers - actually, it's perfectly valid C/C++ to #include a .c or .cpp file, so the distinction between .h and .cpp is largely conventional.

JP's suggestion that putting things in headers might slow down the compile is reasonable but (IME) wrong. ;) In practice, I've found that the bottleneck is the number of compilation units, i.e. .cpp files, not the number or size of headers that it includes. The link step is also a big hit. A project arranged as one .cpp file that includes all of the source compiles (IME) significantly faster than the equivalent independent .cpp files.

In short: you don't have to have one .cpp for every .h. If you know what you're doing then you can do everything in one file. Just don't expect anyone else to understand it.
Last edited by rogerborg on Mon Nov 10, 2008 5:02 pm, edited 1 time in total.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

JP wrote:This is because when you do a #include on a .h file at compile time it copies the entire .h file into that point of the file you've put the #include at. So if your .h files are huge this is going to take a long time to compile. I guess in terms of the resulting application it doesn't matter.. though i'm sure you could run into some nasty compilation issues doing things this way.
I think it's the other way round according to these results. I'd say the main reason to use cpp files is to avoid cyclic dependencys and reduce exe size by not having so many functions inlined.

EDIT: Damn you rogerborg and your quick fingers.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Damn you guys, i'm right and you're wrong! *shakes fist*

I guess rogerborg's comment about doing it all in one file being rather hard to understand for other people is a good reason for using .h and .cpp. In a way it comes down to whether or not you're ever going to let anyone else see it but the best practice is to work in a way that would help you work with other people and show other people your code so that further down the line if that ever happens you're not scarred for life!
Image Image Image
screwgh
Posts: 19
Joined: Tue Oct 14, 2008 12:27 am

Explanation

Post by screwgh »

Hi,

Thanks for all the replys it helped me understand it a lot more. I hired the book C++ in a nutshell at a library but it does not explain this situation at all.

Edit----14-11-08
Hi,

Today started with classes, .cpp and .h files the proper way. Not that the code going to be more clean because you just get more files. But this is the way most people do it. Also I founded a couple of very helpfull links

Hi,

Today started with classes, .cpp and .h files the proper way. Not that the code going to be more clean because you just get more files. But this is the way most people do it. Also I founded a couple of very helpfull links

http://www.gamedev.net/reference/articl ... le1798.asp
http://www.cprogramming.com/faq/cgi-bin ... 1044842972
http://www.digitalmars.com/archives/cplusplus/5076.html
Last edited by screwgh on Fri Nov 14, 2008 4:51 pm, edited 1 time in total.
Post Reply