How do I distinguish between inline and define macros in C++

  • 2020-09-16 07:43:44
  • OfStack

(1) What is an inline function?

Inline functions refer to those member functions defined in the class body, that is, the function body of the function in the class body.

(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 a program.

In addition, earlier we talked about macros, there is such an example:


#define ABS(x) ((x)>0? (x):-(x))

When ++i appears, the macro distorts our meaning, in other words: the definition of a macro can easily produce 2 meanings.

(3) Why can inline replace macros?

1. Inline function of inline defined class. The code of the function is put into the symbol table and directly replaced when used (like macro 1 expansion), without the overhead of invocation and with high efficiency.

2. Obviously, an inline function of a class is also a real function. When the compiler calls an inline function, it first checks the type of its arguments to make sure the call is correct. Then do a series of related checks, just as you would do for any real function 1. This eliminates its hidden dangers and limitations.

3. inline can be used as a member function of a class, of course, you can use the protection of the members and private members of the class.

(4) What is the difference between inline functions and macros?

The difference between an inline function and a macro is that a macro is replaced by a preprocessor, while an inline function is implemented through compiler control. And inlined functions are real functions, only when needed, inlined functions like macro 1 expansion, so the function's argument stack is eliminated, reducing the overhead of the call. You can call inline functions just as you would call function 1 without having to worry about problems with macro 1. Inline function with parameters of macro definitions are compared, and the efficiency of their code is 1 sample, but the gala function is superior to the macro definition, because the type and scope of the inline function follow the rules, it is close to 1, function more, in 1 some compilers, 1 denier shut the inline expansion, with 1 as the calling function 1 samples, more convenient.

(5) When to use inline functions?

Inline functions are probably the most widely used of the C++ class to define access functions. One of the classes we define typically defines data members as private or protected, so that the outside world cannot read or write directly to our class members. Reading and writing to private or protected members must be done using the member interface function. If we define these read and write member functions as inline functions, it will be more efficient.


Class A
{
Private:
int nTest;
 Public:
int readtest() { return nTest;}
void settest(int I) { nTest=I; }
}

(6) How to use 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 of the class is automatically considered inline.

(7) Advantages and disadvantages of inline functions?

We could call it as a function like 1, but because inline functions expand like macro 1 when needed, they actually execute faster than functions like 1. Of course, inline functions have a definite limitation. If the body of the function is too large, the 1-like compiler will abandon the inline method and call the function in the normal way. (In other words, if you use an inline function, you simply make a request to the compiler and the compiler can reject your request.) In this way, inline functions perform as efficiently as normal functions.

(8) How to prohibit function inlining?

If you use VC++, you can use the /Ob command-line argument. Of course, you can also use #pragma auto_inline in your programs for the same purpose.

(9) Matters needing Attention

1. Loop and switch statements are not allowed in inline functions.

2. The definition of an inline function must occur before the first invocation of the inline function.

That's how you can tell the difference between inline and #define macros in C++. For more information on the difference between C++ inline and #define macros, check out the other articles on this site!


Related articles: