Inline function in C ++

Inline functions in C ++ are powerful concepts that are commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each location that the function is called at compile time.

Inline functions in C ++ are powerful concepts that are commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each location that the function is called at compile time.

Any changes to an inline function may require all guests of that function to be recompiled because the compiler will need to replace all the code again, otherwise it will continue with the old feature. .

To make a function is inline, place the inline keyword before the function name and the function definition before any call is created with that function. Compiler can ignore the inline identifier in case the function is defined as more than one line.

A function definition in a class definition is an inline function definition, even if an inline identifier is not used.

The following is an example to use the inline function to return the maximum value of two numbers:

 #include using namespace std ; inline int Max ( int x , int y ) { return ( x > y )? x : y ; } // ham main cua chuong trinh int main ( ) { cout << "Gia tri lon nhat cua (30,20) la: " << Max ( 30 , 20 ) << endl ; cout << "Gia tri lon nhat cua (15,10) la: " << Max ( 15 , 10 ) << endl ; cout << "Gia tri lon nhat cua (120,1230) la: " << Max ( 120 , 1230 ) << endl ; return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

 Gia tri lon nhat cua ( 30 , 20 ) la : 30 Gia tri lon nhat cua ( 15 , 10 ) la : 15 Gia tri lon nhat cua ( 120 , 1230 ) la : 1230 

According to Tutorialspoint

Previous lesson: Friend function in C ++

Next article: Cursor this in C ++

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile