C ++ inline functions (Inline)

Source: Internet
Author: User

The Inline keyword is used to define an inline function of a class. The main reason for introducing it is to use it to replace the macro definition in expression form in C.

An example of macro definition in expression form:

# Define expressionname (var1, var2) (var1) + (var2) * (var1)-(var2 ))

1. Why macro in expression format?

1). High macro execution efficiency. Because it is directly replaced by text at the position required in the Code, there are no parameter pressure stacks, code generation and other operations, which are completed by the pre-processor.

2) function calls require saving and restoring the site, with a certain amount of time and space overhead.

2. disadvantages of Macros in Expression Form

1). Macros cannot access the private and protected members of objects.

2) The macro cannot check the parameter validity, and the return value cannot be forcibly converted.

3). It is easy to produce Ambiguity

3. Advantages of inline

1). High Efficiency similar to macro

2). An inline function is a real function that can access the private and protected members of an object. The compiler can check its parameter validity.

4. When to use inline

1). macro definition in expression form can be completely replaced

2). When the function content is very simple and requires a large number of repeated calls

5. inline usage

1). inline functions must be declared together with the function body to be valid inline int add (int A, int B) {return a + B };

2). In C ++, the function of the function body is defined inside the class and is considered as an inline function by default.

3) inline functions are the most widely used in C ++ classes and should be used to define access functions for private members.

Have you said this today?

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.