The difference and connection of #define \const\inline

Source: Internet
Author: User

Summary:

Const is used instead of a fixed value of # define, and inline is used instead of # define a function. is an upgraded version of # define, in order to eliminate the defect of # define.

the difference between #define和inline

Define: defines a macro that is processed during precompilation ;
simple character substitution, no type detection

typedef: defining type aliases for handling complex types
Example: typedef int A;
Then: a A;//define A as int

Inline: Inline functions advise the compiler whether to replace macros, and the compiler has the right to deny

It is not necessarily successful to make an application

Static
First, create a background
The reason: The variable defined inside the function, when the program executes to its definition, the compiler allocates space on the stack, as you know, the space allocated on the stack is freed at the end of the function execution, which creates a problem: what if you want to save the value of this variable in a function to the next call?
The easiest way to think of is to define a global variable, but there are many drawbacks to defining a global variable, and the most obvious disadvantage is that it destroys the scope of access to the variable (the variable defined in this function is not only controlled by this function). The static members of the class are also the same.
Solution: Therefore, static is introduced in C + +, which modifies the variable to indicate that the compilation
This variable is stored in the static storage area of the program, so that the purpose is achieved and the access scope of the variable is not changed.
Ii. Specific role
static function Analysis Summary: Staticalways makes the storage of variables or objects into static storage , the connection to the internal connection, for the local variable (already connected to the internal), it only changes the way it is stored, for the global variable (already static storage), It only changes its connection type. (1 connection mode: becomes internal connection; 2 Storage form: stored in static global storage)


Const
First, create a background
A C + + has a strict type of compilation system, which makes C + + program errors in the compilation phase can be found a lot, which makes the error rate greatly reduced, therefore, also become C + + and C, has a prominent advantage of one aspect.
A very common preprocessing directive in B C #define VariableName VariableValue can be easily substituted for value, which is a substitute for at least three advantages :


One is to avoid the meaning of vague figures appear, making the program semantics smooth and clear, the following example:
#define USER_NUM_MAX 107 This avoids the confusion of using 107 directly.
Second, the parameters can be easily adjusted and modified, as the above example, when the number from 107 to 201, change here can be;
The third is to improve the execution efficiency of the program, because the use of pre-compiler for value substitution, do not need to allocate storage space for these constants, so the efficiency of execution is high.

However, the preprocessing statement has many advantages, but it has a fatal disadvantage, that is, the preprocessing language
Sentence is just a simple value substitution, lack of type detection mechanism. Such pre-processing statements will not enjoy C + + Strict
The benefits of lattice type checking, which can be a potential cause of a series of errors.


The initial purpose of the CONST rollout is to replace the precompiled directive, eliminating its drawbacks, while
Inherit its advantages.
Now its form has become:
Const DataType VariableName = VariableValue;

Other uses:

2) Specific role


1.const Two-case analysis for pointers:
int const *A; A variable, *a not variable
int *const A; A immutable, *a variable
Analysis: Const is a left-associative type modifier with its left-hand type modifier and a
The type modifier, so that the int const qualifies *A, does not qualify A. int *const limits A, *a is not qualified.


Parameters for passing values of 2.const qualifying functions:
void Fun (const int Var);
Analysis: The above-mentioned method can not be changed in the function body.


Value type return value for 3.const qualifying function:
const int FUN1 ();
Const MyClass FUN2 ();
Analysis: The return value of the above-mentioned function can not be updated, when the function returns the internal type (such as FUN1), is already a numeric value, of course, can not be assigned to update, so, at this time the const meaningless, it is best to remove, lest confusion. When a function returns a custom type (such as Fun2), the type still contains variable members that can be assigned, so it makes sense at this point.


4. Delivery and return address: This is the most common, by the characteristics of address variables, the proper use of const, meaning starkly.


5. Const-qualified member functions of a class:
Class ClassName {
Public
int fun () const;
.....
}
Note: The use of this type of const post form is a requirement and is also intended to not cause confusion. Const is used in both declarations and definitions of this function, because Const has become part of the type information.
Gain Ability: You can manipulate constant objects.
Loss of power: You cannot modify a data member of a class, and you cannot invoke other functions that are not const in a function.


Inline

1) Create a background
The introduction of this keyword is very similar to that of Const, where the inline keyword is used to define an inline function of a class, and the main reason for introducing it is to replace C
The macro definition in the form of an expression.
An example of a macro definition in the form of an expression:
#define EXPRESSIONNAME (VAR1,VAR2) (VAR1+VAR2) * (VAR1-VAR2)
This form of expression is similar to the function of a macro, but it uses a pre-compiler, no stack, and is efficient with the upper-than function. But it is simply a simple substitution of the symbol table on the precompiled compiler, which cannot be used for parameter validation and member access control using C + + classes.
The purpose of the inline rollout is to replace the macro definition in this form of expression, which eliminates its drawbacks and, at the same time, inherits its merits well. The inline code is put into the pre-compiler symbol table and is efficient; it is a real function that has strict parameter detection when invoked; It can also act as a member function of a class.


2) Specific role
Each function member is defined directly in the class definition, which is treated as an inline function, and the member function is an inline function, meaning that each object has a separate copy of the function.
Outside the class, if you use the keyword inline to define a function member, the system is also treated as an inline function;

The difference and connection of #define \const\inline

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.