Cpp Inline Functions
# C++ Inline Functions
[ C++ Classes & Objects](#)
C++ **inline functions** are typically used with classes. If a function is inline, the compiler places a copy of the function's code at each call site during compilation.
Any modification to an inline function requires recompilation of all clients of the function, as the compiler needs to replace all the code again; otherwise, the old function will continue to be used.
To define a function as an inline function, place the keyword **inline** before the function name, and the function must be defined before it is called. If the defined function has more than one line, the compiler ignores the inline qualifier.
Functions defined within a class definition are inline functions, even without the **inline** specifier.
Here is an example that uses an inline function to return the larger of two numbers:
#include using namespace std;inline int Max(int x, int y){ return (x > y)? x : y;}// Main function of the programint main( ){ cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0;}
When the above code is compiled and executed, it produces the following result:
Max (20,10): 20Max (0,200): 200Max (100,1010): 1010
[ C++ Classes & Objects](#)
YouTip