×

C++ Inline Functions


What are Inline Functions ?

To clean up the code we write functions outside the class and this is best way to write any object oriented code. So why Inline Function ?. When we declare any function as inline then while compilation, compiler will replace that call of a function with the exact code whic you had written outside of that class.

In this example , compiler will replace the statement ,

inline void display();
with
void Game :: display()
{
  cout<<abc;
}

Benefit of this inline function is to increase the execution speed of a program . By writing inline it skips the step of function call which actually takes overhead in memory and replaces that line with actual code. So in this way code looks cleaned plus memory saving :)

#include <iostream>
using namespace std;
class Game
{
public:
  int abc;

Game()
{
  abc = 5;
}
inline void display();
}; 

void Game :: display()
{
  cout<<abc;
}

int main()
{
 Game sankeObject;     // Creatig Game class's object
 sankeObject.display();
}

Output :-

5