Inline Functions

To avoid the overhead associated with a function call (creation of a stack frame containing copies of arguments or addresses of reference parameters and the return address) C++ permits you to declare functions to be inline. Such a declaration is a request to the compiler that it replace each call to the function with the fully expanded code of the function. For example:

inline int max(int a, int b){
 return a > b ? a : b ;

int main(){
 int temp = max(3,5);

The compiler could substitute the expanded code for max as shown here.

int main() {
 int temp;
 int a = 3;
 int b = 5;
 temp = a > b ? a : b;

The inlining of a function can give a significant boost in performance if it is called repeatedly (e.g., inside a large loop). The penalty for inlining a function is that it might make the compiled code larger, which will cause the program to use more memory while it is running. For small functions that get called many times, that memory effect will be small while the potential performance gain might be large.

There are no simple answers to the question of whether inlining will improve the performance of your program or not. A lot depends on the optimization settings of the compiler. A lot depends on the nature of the program. Does it make very heavy use of the processor? Does it make heavy use of system memory? Does it spend a lot of its time interacting with slow devices (e.g., input and output)? The answers to these questions affect the answer to the question of whether or not to inline, and we leave them to a more advanced treatment of the subject. For an overview of the complexity of this issue, visit Marshall Cline's FAQ Lite site.[2]


An inline function is similar to a #define macro with one very important difference: The substitution process for a #define macro is handled by the preprocessor, which is essentially a text editor. The substitution process for an inline function is handled by the compiler, which will perform the operation much more intelligently with proper type checking. We discuss this in more detail in the next section.

Some Rules about Inline Functions

  • An inline function must be defined before it is called (a declaration is not enough).
  • An inline definition can only occur once in any source code module.
  • If a class member function's definition appears inside the class definition, the function is implicitly inline.

If a function is too complex, or the compiler options are switched, the compiler may ignore the inline directive. Most compilers refuse to inline functions that contain:

  • while, for, do . . . while statements
  • switch statements
  • More than a certain number of lines of code

If the compiler does refuse to inline a function, it treats it as a normal function and generates regular function calls.

Part I: Introduction to C++ and Qt 4

C++ Introduction


Introduction to Qt



Inheritance and Polymorphism

Part II: Higher-Level Programming


Introduction to Design Patterns


Generics and Containers

Qt GUI Widgets


Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments


MP3 Jukebox Assignments

show all menu

An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: