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.
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 ++
You should read it
May be interested
- Cursor this in C ++each object in c ++ has access to its own location through an important pointer called this pointer. the pointer this in c ++ is a hidden parameter to all member functions. so inside a member function, this pointer can refer to the calling object.
- Cursor to class in C ++a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.
- Static member of class in C ++we can define class members as static using the static keyword in c ++. when we declare a member of a class that is static, that is, no matter how many objects of the class are created, there will only be one copy of the static member.
- Operator overload and Load overlap in C ++c ++ allows you to define more than one definition for a function name or an operator in the scope, respectively called load overloading (function overloading) and load operator overloading in c ++. .
- Overload the one-seat operator in C ++the unary operator in c ++ operates on a single operand.
- Overload binary operators in C ++binary operators in c ++ take two parameters. you use binary operators quite often, such as addition operator (+), subtraction operator (-) and division operator (/).