Inline Function in C++ - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Inline Function in C++

Share This

Inline function is an important feature in C++, which is commonly used with classes. These are the short functions, where the codes are expanded in line at the point of each invocation. This process is similar to using a function-like macro. To cause a function to be expanded in line rather than called, precede its definition with the inline keyword.

For example, in this program, the function max() is expanded in line instead of called:


#include <iostream> using namespace std; inline int max(int a, int b) { return a > b ? a : b; } int main() { cout << max(10, 20); cout << " " << max(99, 88); return 0; }

As far as the compiler is concerned, the preceding program is equivalent to this one:


#include <iostream> using namespace std; int main() { cout << (10 > 20 ? 10 : 20); cout << " " << (99 > 88 ? 99 : 88); return 0; }


Happy Exploring!

No comments:

Post a Comment