1. Why Use inline
On most machines, the calling function does a lot of work: Save the Register before the call, restore it on return, copy the arguments, and the program must also move to a new location to execute.
Use inline functions to avoid the overhead of a function call.
Inline instructions are just a suggestion for the compiler, and the compiler can choose to ignore this recommendation.
2. Inline function definition
The definition of an inline function must be visible to the compiler so that the compiler can expand the code of the function in the call point. At this point, only functional prototypes are not enough.
Inline functions may have to be defined more than once in a program, as long as the definition of an inline function appears only once in a source file, and in all source files, the definition must be exactly the same. Placing the definition of an inline function in a header file ensures that when the function is invoked, the same definition is used. and ensure that the definition of the function at the call point is visible to the compiler.
Inline functions should be defined in the header file (recommended).
3. Inline functions in a class
member functions that are defined within a class are automatically treated as inline.
You can define a member as a inline in the class definition body as part of its declaration. Alternatively, you can specify inline on a function definition outside of the class definition body. Specifying inline at the declaration and definition is legal.
An inline member function that is not defined in the class definition body, whose definition is usually placed in the same head file with the class definition.
Class screen
{public
:
typedef std::string::size_type Index;
Implicitly inline where defined inside the class declaration
char get () const {return CONTENTS[CUROSR];}
Explicitly declared as inline, willl is defined outside the class declaration
inline char get (index HT, index WD) c Onst;
Inline not specified in class declaration, but can is defined inline
index later () const;
Inline declared in the class declaration; No need
to repeat on the definition Char screen::get (index R, index c) cosnt
{
index row = r* width;
return Contents[row + c];
}
Not declared as inline into the class declaration, but OK to do inline in definition
inline screen::index Et_cirsot () const
{return
cursor
}
Reference :effective C + +, clause 30: Thorough understanding of inlining