functional code in .h files

Get answers to all your basic programming questions. No Ogre questions, please!
golembuilder
Gnoblar
Posts: 13
Joined: Fri Apr 20, 2012 4:37 am
Location: Hangzhou, China

functional code in .h files

Post by golembuilder »

Hi everyone,

While following some of the tutorials about Ogre, some of them would recommend looking at specific examples in the Ogre sample stuff. However, I got confused when looking at the .cpp files for Sinbad, grass, etc. because it just had some functions for plugins. I was rather surprised to find all the code iin the header files. I'm a CS student, and still learning... well everything. I remember being told that you can put functional stuff in the header files, but that it's not common/recommended. So, other than the examples stuff, is it actually more common to put functional code in the header files? I have seen people put the simple setters/getters in the header files before. This isn't a criticism of Ogre or anything, I'm generally curious about it.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: functional code in .h files

Post by bstone »

I guess that was just a convenience. The simpler to create a sample, the more likely it will make it and come alive :D For things like samples it's just a way to reduce some typing overhead. For more involved projects it's better to keep declarations and implementations separate for a few reasons unless you create something 100% template based or some primitive, final, value-type class that can be fully inlined.
golembuilder
Gnoblar
Posts: 13
Joined: Fri Apr 20, 2012 4:37 am
Location: Hangzhou, China

Re: functional code in .h files

Post by golembuilder »

Hey bstone,

Yeah I can understand a little why they did it that way, I just have so little experience with this kind of set up that it never occurred to me to check the .h files for the samples haha. It was a little confusing/frustrating for the first bit when I was trying to figure out how Sinbad worked (in relation to materials, animations, weapon attachments, etc.) But, the more I go through stuff here, the more I learn :)
User avatar
Miscreant
Greenskin
Posts: 126
Joined: Mon Jun 14, 2010 2:12 am
Location: Brisbane, Australia
x 3

Re: functional code in .h files

Post by Miscreant »

Well it doesn't really matter for the samples since those functions are only included once, by one source file. When you start getting into functions that are in header files included in multiple source (.cpp/.cxx) files it becomes more of an issue, since when you write a function in a source file it only gets compiled once, whereas if you write it in a header file it will need to be compiled for as many times as that header is included. Essentially what this means is that it can save time typing, but if you need to include that function in multiple source files it can increase compile time.
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: functional code in .h files

Post by lingfors »

Also, there can be problems with some tools not properly detecting when a .h file has been updated... So, you change the code in an .h file, but the changes doesn't make it into the compiled binary because the cpp files that included the h file wasn't recompiled...

Or even worse, some cpp files are recompiled, some others aren't...
golembuilder
Gnoblar
Posts: 13
Joined: Fri Apr 20, 2012 4:37 am
Location: Hangzhou, China

Re: functional code in .h files

Post by golembuilder »

Oh, that's interesting. Thanks for the info :)