Hi All programmers/reviewers,
I hope you would be aware of inline functions in C++. Here I will not discuss about "what is inline" and explain it, rather I would tell few important (in-line) about inline functions.
For information about inline functions, you can get the data using the links like
1) http://www.parashift.com/c++-faq-lite/inline-functions.html
2) http://www.codersource.net/cpp_tutorial_inline_functions.html
3) Inline or NOT to inline in C++ http://www.thescripts.com/forum/thread59977.html
4) Wikipedia http://en.wikipedia.org/wiki/Inline_function
Let's take an example
//Declaration for C++ Tutorial inline sample:
inline int add(int x,int y);
If you are working on the code, you would have find that in most of the code, the inline function defintion is in the header file with declaration
//Definition for C++ Tutorial inline sample:
inline int add(int x,int y)
{
return x+y;
}
Why is it so ??
Let's take a simple example that the definition and declaration does not lie in the same file. Declaration lies in header file math.h and Definition lies in source file math.cpp
Let's go in the important property of inline functions.
It replaces the function called with the function contents inside the module. Here there is one condition attached that the inline function would work only in the same module e.g. if you have a module Mathematics which declares and defines the inline function add(int, int) and you have a different module called as Physics (has some file as phy.cpp) which calls the inline function as
s = add (a,b);
then the compiler (especially in Linux) is going to throw the reference error .. YES !! The reason is that the compiler is unable to open the function becasue the function is defined as inline in the other module.
How to Solve such issues ?
There are two ways to get the solution
1) Make the function as NOT inline. i.e. make the function as
int add(int x,int y) in declaration and define it in definition.
2) Declare and Define the function as inline in the header file itself. and then include this header file wherever the function add(int,int) is being called. This will ensure that the function add is also expanded as inline :)
So phy.cpp would be containing the line #include "math.h"
Enjoy !!
Adhir
Subscribe to:
Post Comments (Atom)
2 comments:
I absolutely love your blog and find many of your post's to be what precisely I'm looking for.
Do you offer guest writers to write content for you personally?
I wouldn't mind producing a post or elaborating on most of the subjects you write in relation to here. Again, awesome weblog!
Visit my page :: how to download movies
Really very happy to say,your post is very interesting to read. I never stop myself to say something about it.You’re doing a great job.Keep it up.
mahabaleshwar packages
mahabaleshwar tour package
Post a Comment