< previous page page_77 next page >

Page 77
What's the rule of thumb? If you have a small function, one or two statements, it is a candidate for inline. When in doubt, though, leave it out. Listing 5.6 demonstrates an inline function.
LISTING 5.6 DEMONSTRATES AN INLINE FUNCTION

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:   // Listing 5.6 - demonstrates inline functions
2:
3:   #include <iostream.h>
4:
5:   inline int Double(int);
6:
7:   int main()
8:   {
9:     int target;
10:
11:    cout << Enter a number to work with: ;
12:    cin >> target;
13:    cout << \n;
14:
15:    target = Double(target);
16:    cout << Target:  << target << endl;
17:
18:    target = Double(target);
19:    cout << Target:  << target << endl;
20:
21:
22:    target = Double(target);
23:    cout << Target:  << target << endl;
24:       return 0;
25:  }
26:
27:  int Double(int target)
28:  {
29:    return 2*target;
30:  }

Output:
Enter a number to work with: 20
Target: 40
Target: 80
Target: 160
Analysis: On line 5, Double() is declared to be an inline function taking an int parameter and returning an int. The declaration is just like any other prototype except that the keyword inline is prepended just before the return value.

 
< previous page page_77 next page >

If you like this book, buy it!