[C++] Inline Functions Tutorial - Printable Version +- howtothings.co.uk (https://www.howtothings.co.uk) +-- Forum: Computing (https://www.howtothings.co.uk/forumdisplay.php?fid=4) +--- Forum: Programming Support, Graphic Design and Tutorials (https://www.howtothings.co.uk/forumdisplay.php?fid=14) +--- Thread: [C++] Inline Functions Tutorial (/showthread.php?tid=1119) |
[C++] Inline Functions Tutorial - Clones - 11-11-2011 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() { Now, we are going to explain how to implement inline functions inside classes... Look at this very simple class: Code: class simple { 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? Look at the updated class: Code: class simple { 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 { 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 * RE: [C++] Inline Functions Tutorial - annaharris - 11-09-2012 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. |