The difference between C + + inline and # define macros

Source: Internet
Author: User

(1) What is an inline function?

An inline function is a member function that is defined within a class, that is, the function body of the function is placed inside the class.

(2) Why inline functions are introduced?
Of course, the main purpose of introducing inline functions is to solve the efficiency problem of function calls in the program.
In addition, we talked about the macro in the front, there is such an example:
#define ABS (x) ((x) >0? (x):-(x))
When ++i appears, the macro will distort our meaning, in other words: the definition of a macro can easily produce two of meaning.
  

(3) Why does the inline replace the macro?
1, inline functions of the class defined by the, function's code is placed in the symbol table , and when used directly
Replace, (like a macro expansion), without the cost of the call, the efficiency is very high.  
2, it is obvious that the inline function of a class is also a true function, and when the compiler calls an inline function, it
First checks the type of its arguments to ensure that the call is correct. Then perform a series of related checks, just like any
real function. This eliminates its hidden dangers and limitations.
3, inline can function as a member of a class, and of course you can use the protected member
and private member of the class in which it resides.

(4) What is the difference between an inline function and a macro?
The difference between an inline function and a macro is that the macro is overridden by the preprocessor , and the inline function is through the compiler
control to implement . and the inline function is the real function, but when needed, the inline function expands like a macro
, so cancels the function's argument stack, reducing the cost of the call . You can call the
inline function just as you would call a function, without worrying about some of the problems that might arise from handling the macro. The
inline function is compared with the macro definition with parameters, their code efficiency is the same, but the inside function is
better than the macro definition, because the inline function follows the type and scope rules, it is more similar to the general function, in some compilation
, once the inline extension is closed, It is convenient to make the same call as the general function.

(5) When is an inline function used?
Inline functions are most widely used in C + + classes and should be used to define access functions. The classes we define are generally
Data members are defined as private or protected so that the outside world cannot read and write data about our class members directly.
。 You must use member interface functions to read and write to private or protected members. If we write these readings,
function is defined as an inline function, it will get better efficiency.
Class A
{
Private:
int ntest;
Public:
int Readtest () {return ntest;}
void settest (int I) {ntest=i;}
}

(6) How do I use an inline function?
We can use inline to define inline functions.
inline int A (int x) {return 2*x;}
However, any function defined in the Description section of the class is automatically considered an inline function.

(7) Advantages and disadvantages of inline functions?
We can call it as a normal function, but since the inline function will be like Hongyi when needed
Execution speed is faster than the normal function.
Of course, the inline function also has some limitations. is that the execution code in the function cannot be too much , if, inline
function body is too large, the general compiler will discard the inline mode, and the normal way to call the function. (in other words,
That is, you use an inline function, just to make a request to the compiler, the compiler can reject your application)

In this way, the inline function is as efficient as the normal function execution.

(8) How do I disable the inline function?
If you use VC + +, you can use the/OB command line parameter. Of course, you can also use #pragma auto_inline in your program to achieve the same goal.

(9) Precautions:
1. loop Statements and switch statements are not allowed within inline functions.
2. the definition of an inline function must appear before the inline function is first called.

The difference between C + + inline and # define macros

Related Article

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.