Friday, May 30, 2008
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)

1 comment :

  1. Don't use inline keyword when you define the function inside the class because the function defined inside the class is implicitly inline. If you want to explicitly make it inline then just declare the function inside class and use inline keyword when you define it outside the class using :: operator. This example shows bad programming style

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top