• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++] Inline Functions Tutorial
#1
Introduction

In C, one of the ways to preserve efficiency is through the use of macros, which allows you to make what looks like a function without the normal function call's overhead. Macros are replaced with macro codes by preprocessor.
There are some problems here, with using macro: First, No type checking is done when replacing macros. Second, macros cannot access private class members. So... what happens to the efficiency I said ? This was solved by bringing in the concept of inline functions...

Syntax

Making a C++ function inline is so easy, the function definition is the same as normal functions, just add the inline keyword before the return type. This simple function is defined as inline:

Code:
inline void func() {
     cout<<"inline function"<<endl;
}

Now, we are going to explain how to implement inline functions inside classes... Look at this very simple class:

Code:
class simple {
     int val;
public:
     int ret();
     void set(const int& a);
};

int simple::ret() {
     return val;
}

void simple::set(const int& a) {
     val = a;
}

In this class, there are two very basic member functions named ret and set, the first one returns the value of the internal variable and the second one assigns the internal value a given argument. Such little/simple functions should be defined as inline to avoid having function call overhead for them... which helps improve the speed and efficiency of our class.

How do we define member functions as inline? the answer is: Just define them right inside the class body; all functions defined inside class' body are treated as inline functions... how ever you Can add the inline keyword to the function definition also, but why not make it shorter? Big Grin
Look at the updated class:

Code:
class simple {
     int val;
public:
     //inline function
     int ret() {
         return val;
     }
     //inline function
     void set(const int& a) {
         val = a;
     }
};

So, now these two member functions are inline. If you want to keep the class interface clean, you can have the same normal prototype in class interface and have the inline keyword in definition

Code:
class simple {
     int val;
public:
     int ret();
     void set(const int& a);
};

inline int simple::ret() {
     return val;
}

inline void simple::set(const int& a) {
     val = a;
}

Just note that, The inline code Does occupy space, but if the function is small, this can actually take less space than the code generated to do an ordinary function call.
OK, now speak about the problems and limitations...

A Compiler can only make a function inline if the definition of the function exits within the same translation unit as the call, which means inside the same source/header file it was defined, plus header files included inside this file.
Also note that, by putting the inline keyword (or by other ways mentioned), there is no guarantee that they will be made inline, as it is Only a Suggestion to the compiler... after this suggestion, the compiler decides whether or not it can go inline. Always note that, very simple functions that have no loops(looping is counted as a complicated operation for replacement) should be defined as inline to give you efficiency. But as mentioned, it is a suggestion and the compiler is the final decider. *did you notice I said that note two times in a row? because it was important Big Grin*
  Reply
#2
Although I already learned about basic functions in c++ the inline function. Inline functions are not always important, but it is nice to understand them. The basic idea is to save time at a cost in space. Inline functions are a lot like a placeholder. One time you define an inline function, using the 'inline' keyword, everytime you call that function the compiler will replace the function call with the actual code from the function.
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Drumms Useful Functions! Drumm 4 10,620 08-10-2011, 10:36 PM
Last Post: Mark
  Two functions for the ELU Rating system Drumm 8 21,695 02-03-2011, 09:57 AM
Last Post: Drumm

Forum Jump: