Sign in to follow this  
dpadam450

header files auto inline?

Recommended Posts

dpadam450    2357
I was just wondering if declaring and defining a function in a header makes it inline? Some friends were saying this and I wasn't sure. If they aren't inline, then why is there always a .h and .ccp file?

Share this post


Link to post
Share on other sites
Norling    166
Functions are inline if you explicitly declare them as inline, or if you define them within a class definition. Since most class definitions are in header files, that is probably what your friends meant. Inline functions must also be defined in header files, or in the same .cpp file where it is being used, since the compiler needs to know the function body to be able to inline it.

Are you asking the purpose of separating declarations and definitions into .h and .cpp files? A short answer is that you need a visible declaration of every function and class you want to use, so you need to include it as a header in the file you want to use it. However, you only want the function definition (implementation) specified one place in the program, in it's own file. What extension you use for the files are irrelevant, but the convention is to use .h and .cpp .
If you were to write the whole program in one file, you wouldn't have to separate it into a header and a definition file, but then it would become immensely difficult to maintain once it reached some hundred lines.

Share this post


Link to post
Share on other sites
Twinsen2    110
Yeah man, funcs defined in the header are automatic inlined, u dont even have to put "inline" at the front if thier defined in the header.
Some people know this and still put it there for better syntax and so other people who may not know this can see the word inline.

You only really want to inline small and common functions thou, so like if you have a func with a few lines that gets called alot, then i define it in the header, as stated in post above, it does have to be in the class def.

Y do we need h & cpp files? well from my understanding to break it all up, so objs have thier on files and u link the ones u need in...now when i started o wrote a opengl app all in main, was using goto's as i didnt understand funcs back then :S, now thou, i am writting pretty complex games and if it was all in the one file i would go insane and given up long ago, when in seperate files its soo much easier, if i want to look at the code for character inventory, i have a file named inventory for example and its all right there, no need for searching through massive amounts of code for the thing you want.

Using the Find tool aswell would work, but when writing many functions it would quickly become frustrating to say the least.

hope that helped.

Share this post


Link to post
Share on other sites
Skizz    794
It is a common misconception that inline functions, declared with the inline keyword or defined in the header file, are always inlined. From MSDN:
Quote:

The insertion (called inline expansion or inlining) occurs only if the compiler's cost/benefit analysis show it to be profitable.

The compiler treats the inline expansion options and keywords as suggestions. There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword.


The __forceinline keyword is MS specific.

Skizz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this