C + + inline functions

Source: Internet
Author: User
Tags class definition

About inline functions

Keywords:inline

C + + inline functions are typically used with classes. If a function is inline, at compile time, the compiler places a copy of the function's code in each call to that function.

Any modification to the inline function requires that all clients of the function be recompiled, because the compiler needs to replace all of the code once, otherwise the old function will continue to be used.

If you want to define a function as an inline function, you need to place the keyword inlinebefore the function name, and you need to define the function before calling the function. If there are more than one row of defined functions, the compiler ignores the inline qualifier.

functions defined in a class definition are inline functions even if the inline specifier is not used.

Here is an example that uses an inline function to return the maximum value in two numbers:

#include <iostream>using namespaceStd;inlineintMax (intXinty) {   return(x > Y)? x:y;//Returns x if x>y, otherwise returns y}//main function of the programintMain () {cout<<"Max (20,10):"<< Max ( -,Ten) <<Endl; cout<<"Max (0,200):"<< Max (0, $) <<Endl; cout<<"Max (100,1010):"<< Max ( -,1010) <<Endl; return 0;}

When the above code is compiled and executed, it produces the following results:

Max (0,tenmax (100 ,10101010
The difference between an inline function and a normal function

Inline functions must be simple code so that the internal statements of the function can be compiled directly, and if the function is too cumbersome, even adding inline does not compile in the way of inline functions. In addition, the loop body function cannot be compiled into an inline function.

In-class definitions and inline functions

As the function name and function body are written in the class inside the function form is called the definition of the class, the compiler will compile it by default to an inline function.

Class student{public:    void Setage (int. _age) {age =int getage () {returnvoid  Study () {}privatestringint age ;} ;            

C + + inline functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.