[C ++] inline functions (inline) and macro definitions (# define) and the difference between the pros and cons

A macro definition: # define

1. Why should you use macros?

Because the call to the called function is more efficient than macro call, the function must be performed in the order you want to program transferred to the memory address of the function stored in the function after the implementation of the program content, and then return to the place of execution of the function before, this required to perform before the transfer operation to save the site and perform memory address, to be restored back after the scene, according to the original address continue to save , therefore, function calls have some time and space overhead , but only in macro preprocessing place expand the code does not require extra time and space overhead, calling a macro function is more efficient than calling a

2. The greatest benefit of macro definitions:

The use of a macro function is defined as, but not function, it is implemented using a pre-processor, not the pushing of the parameters, code generation and a series of operations, and therefore high efficiency

3. macro definition drawbacks:

1) Although the macro defines a similar function, but in use, just do preprocessor symbol table simple substitution, so it can not be detected parameters of effectiveness , it can not enjoy the benefits of C ++ compiler strict type checking , in addition, it will return value can not be cast to the appropriate type of switchable

2) macro definition member variables can not access the class

3) When using the macro definition parameters, strict replacement policy, when the parameter whether you have what form, is used instead of the expanded code-size parameter argument, so that the macro definition is easy to produce ambiguous , it use there is a series of risks


Two inline functions:. Inline

1. Introduction inline functions:

Inline function layer from the source code to see, there is a function of structure, after compilation, but do not have a function of the nature of the associated function is not invoked during transfer of control occurs, but at compile time function element is fitted at each call, the compiler when a similar macro substitution, use the function body to replace the function name to call at , inline keyword modifications in the code used in member functions declared in the class, automatically converted to inline functions

1) class member function inline default ( requires no member function and recursive loop, wherein when the presence of a recursive loop, the compiler will default to ordinary functions a )

2) inline function does not allow the statement or a recursive loop

3) the definition of an inline function must appear before the first call to an inline function

2. inline functions advantages:

1) There are parameters type detection, safer

2) inline function is expanded when the program is run, and the parameter is passed

3) inline keyword is only a definition of the compiler, if the function does not meet local standards inline function, the compiler will function as an ordinary function

3. inline functions disadvantages:

1) Because the inline function is expanded at the call, so the code will make the long side, take up more memory


Three main difference between an inline function and macro definitions:

1) may be inline function at run-time debugging, not the macro definition

2) Parameter Type compiler would inline function within the security check or automatic type conversions, the macro is not defined

3) inline function can access the class member variable, and is not macro definition


Guess you like

Origin www.cnblogs.com/yinbiao/p/11606554.html