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.
Code in .h or .cpp files
-
DominicDS
- Gnoblar
- Posts: 3
- Joined: Sat Oct 30, 2004 8:10 pm
- Location: Florida, USA
-
haffax
- OGRE Retired Moderator

- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 8
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.
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.
-
DWORD
- OGRE Retired Moderator

- Posts: 1365
- Joined: Tue Sep 07, 2004 12:43 pm
- Location: Aalborg, Denmark
-
Kencho
- OGRE Retired Moderator

- Posts: 4011
- Joined: Fri Sep 19, 2003 6:28 pm
- Location: Burgos, Spain
- x 2
-
haffax
- OGRE Retired Moderator

- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 8
-
DWORD
- OGRE Retired Moderator

- Posts: 1365
- Joined: Tue Sep 07, 2004 12:43 pm
- Location: Aalborg, Denmark
Yup. That's why I said 'can't be in .cpp files'.Kencho wrote:Correction: You can declare inline functions in a separate file by using the keyword 'inline'
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.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.
Edit: Defining Inline C++ Functions with dllexport and dllimport. Whatever that means. Can someone clever please translate?
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.
-
Kojack
- OGRE Moderator

- Posts: 7157
- Joined: Sun Jan 25, 2004 7:35 am
- Location: Brisbane, Australia
- x 538
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).
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

- Posts: 198
- Joined: Sat Oct 02, 2004 9:11 am
- Location: San Francisco, California, USA
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.
And no, functions /cannot/ be made inline if they are compiled into a DLL. You have to place those functions in a header.
-
DWORD
- OGRE Retired Moderator

- Posts: 1365
- Joined: Tue Sep 07, 2004 12:43 pm
- Location: Aalborg, Denmark
@Kojack: Thanks for the clarification. 

Yes ofcourse.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).
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.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.
