Code in .h or .cpp files

Get answers to all your basic programming questions. No Ogre questions, please!
DominicDS
Gnoblar
Posts: 3
Joined: Sat Oct 30, 2004 8:10 pm
Location: Florida, USA

Code in .h or .cpp files

Post by DominicDS »

Hi everyone,
I have a simple question regarding the placement of method implementations. When I was learning C++, I was taught that member function declarations (headers) should be placed in the .h file and all of the functino code should be placed in an separate .cpp file. I have noticed that in Ogre (the samples especially), large functions are placed entirely in the .h files.

Why is this done? Are there specific practices when it comes to placing member function implementations?

Thanks.
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8

Post by haffax »

The Ogre samples are created in the simplest possible way, so implementations are in the header files.

Imho, it is best to implement all methods (even small one liners) in a .cpp file. So you always know where to find the definition and where the declaration. Ogre samples don't necessarily reflect best practices for C++ programming.
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark

Post by DWORD »

I agree with tanis, member functions go into .cpp files. One exception, though, is inline functions which are used where speed is important. Inline functions can't be in .cpp files; you'll see that e.g. implementations of vector classes often use inline functions.
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2

Post by Kencho »

Correction: You can declare inline functions in a separate file by using the keyword 'inline' ;)
Image
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8

Post by haffax »

Does the inline keyowrd work over DLLs too? When I declare a method inline in a file, that is part of a DLL. And then I call this function in an executable that uses this DLL. Is the function inlined or not? I guess not, but don't really know.
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark

Post by DWORD »

Kencho wrote:Correction: You can declare inline functions in a separate file by using the keyword 'inline' ;)
Yup. That's why I said 'can't be in .cpp files'. ;) Often they're in the bottom of 'class.h' or separately in 'class.inl'. But functions implemented inside the class declaration are implicitly 'inline'.
tanis wrote:Does the inline keyowrd work over DLLs too? When I declare a method inline in a file, that is part of a DLL. And then I call this function in an executable that uses this DLL. Is the function inlined or not? I guess not, but don't really know.
I don't really know either, but I would like to. I guess they could be inlined, because the point in inlining is postponing (right word?) the compilation of the function until it's actually used in code, to avoid the function call overhead. So inlined functions should not be compiled into the DLL, but instead be compiled wherever they're used. I don't think this is how it works, though. :roll: The 'inline' keyword is probably just ignored when exporting the function in a DLL.

Edit: Defining Inline C++ Functions with dllexport and dllimport. Whatever that means. Can someone clever please translate? :P

What I get from this is something along that you can use inline functions in DLLs, but they're always compiled and exported anyway. If the program using the DLL imports with the inline keyword, then the function will be inlined. Please correct me if I'm wrong.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Post by Kojack »

Inline functions can certainly be in cpp files, but they only apply to that one cpp (and cause linking errors if you try to call them from other cpp files).

Inline is only a suggestion. The compiler won't always inline if it thinks it's a bad idea (visual studio help lists a bunch of cases where inlining won't be used).

For dll's, inline functions will work as normal in your code. A copy of the inlined function is stored in the dll so you can call the function by a pointer, but otherwise your code will use it just like a normal inline function. So the dll gets an inline version, the client of the dll gets an inline version, and a non inline version is also stored in the dll for when needed (like when using a function pointer).

If you change an inline function in a dll and recompile just the dll, then anything linking with that dll must be recompiled too otherwise it won't pick up the changes since it's already expanded the old version of the function. Unless it used a pointer, in which case you could have both an old and new version used at once (pointer access using the new version, regular access using the old version).
qsilver
OGRE Community Helper
OGRE Community Helper
Posts: 198
Joined: Sat Oct 02, 2004 9:11 am
Location: San Francisco, California, USA

Post by qsilver »

Inline functions (one or two lines) should always go in the header, *unless* you use the latest Visual Studio .NET, in which case you should put them in .CPP files and turn on "Global Optimization" for best results.


And no, functions /cannot/ be made inline if they are compiled into a DLL. You have to place those functions in a header.
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark

Post by DWORD »

@Kojack: Thanks for the clarification. :)
Kojack wrote:Inline functions can certainly be in cpp files, but they only apply to that one cpp (and cause linking errors if you try to call them from other cpp files).
Yes ofcourse. :)
qsilver wrote:And no, functions /cannot/ be made inline if they are compiled into a DLL. You have to place those functions in a header.
They must go into a header only if you want to use the inlined version from outside the DLL, but they'll always get compiled into the DLL as a normal function, too, so you can get a function pointer.