Friday, May 30, 2008

C++ Inline Functions

Leave a Comment
What are inline functions?
  • Inline functions help to avoid the performance overhead associated with function calling when there are too many calls to a really small function.
  • Code of an inline function would be inserted at every point of function call by the compiler.
  • Adding the keyword inline is just an instruction to the compiler and there is no guarnatee.
  • Inline functions are similar to #define but they have better type checking.
  • If the function is defined within the class declaration and it is small the compiler may automatically decide to make the function inline.

EXAMPLE: Demonstrate the usage of inline functions


#include <iostream>
using namespace std;

class MyClass {
int data;
public:
MyClass() { data = 100; };
inline int GetData() { return data; };
};

int main()
{
MyClass obj;
for ( int i = 0; i < 100; i++ )
{
cout << obj.GetData() << endl;
}
}

OUTPUT:
100 (100 times)
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment