Difference between define and inline

Source: Internet
Author: User
Define: Define macros processed during pre-Compilation;
  Only simple character replacement, no type detection

Typedef:Define the type alias for processing complex types
 Example: typedef int;
 Then: A; // defines a as Int.

Inline:The inline function provides suggestions to the compiler. the compiler has the right to reject the macro replacement.

 Not necessarily succeeded in submitting an application

 

Static
I. background
Cause: when the program executes the variable defined in the function to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it?
The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ). This is also true for static members of the class.
Solution: Therefore, static is introduced in C ++ to modify variables, which can indicate compilation.
The variable is stored in the static storage area of the program. This achieves the goal and keeps the access range of the variable unchanged.
II. Specific functions
Static analysis summary: static always changes the storage form of variables or objects to static storage, and the connection mode to internal connections. For local variables (internal connections already exist ), it only changes the storage mode. For global variables (which are already in static storage), it only changes the connection type. (1 connection mode: internal connection; 2 storage mode: stored in the static global Storage Area)


Const
I. background
A c ++ has a strict compilation system, which allows many errors of C ++ programs to be found in the compilation phase, thus greatly reducing the error rate, it has also become an outstanding advantage in comparison with C ++.
A common preprocessing command in B C # define variablename variablevalue can be easily used for value substitution. This value substitution has at least three advantages:
The first is to avoid the appearance of numbers with vague meanings, so that the program semantics is fluent and clear, as shown in the following example:
# Define user_num_max 107 avoids the confusion caused by using 107 directly.
Second, you can easily adjust and modify parameters. In the preceding example, when the number of people changes from 107 to 201, you can modify the parameters here;
Third, it improves the execution efficiency of the program. Because the pre-compiler is used for value substitution, it does not need to allocate storage space for these constants, so the execution efficiency is high.

However, a preprocessing statement has many advantages, but it has a fatal disadvantage, that is, preprocessing
The sentence is only a replacement of simple values, and there is no type detection mechanism. In this way, the pre-processing statement cannot enjoy strict C ++
The benefits of lattice type check may become a series of errors.
The initial purpose of const is to replace precompiled commands and eliminate its shortcomings.
It inherits its advantages.
Now its form is changed:
Const datatype variablename = variablevalue;
2) specific functions
1. const is used for Pointer Analysis in two cases:
  Int const * A; // a variable, * a variable
  Int * const A; // A is not variable, * A is variable
Analysis: const is a type modifier that is left-bound. It is the same as its type Modifier on the left.
Type modifier. Therefore, int const is limited to * a, not. Int * const limits a, not *.
2. Const limits the passing value parameters of the function:
  Void fun (const int var );
  Analysis: The preceding statement specifies that parameters cannot be changed in the function body.
3. Const limits the return value of the function:
Const int fun1 ();
Const myclass fun2 ();
  Analysis: The preceding statement limits that the return value of a function cannot be updated. When the function returns an internal type (for example, fun1), it is already a value. Of course, it cannot be assigned an update, at this time, const is meaningless. It is best to remove it to avoid confusion. When the function returns a custom type (such as fun2), this type still contains variable members that can be assigned values, so it makes sense at this time.
4. Transfer and return address: This is the most common case. It can be seen from the characteristics of address variables that the appropriate use of const is significant.
5. Const qualified class member functions:
Class classname {
Public:
Int fun () const;
.....
}
Note: This post-const form is a provision to avoid confusion. Use const in the declaration and definition of this function, because const has become part of the type information.
Ability to obtain: constant objects can be operated.
Capacity Loss: you cannot modify the data members of a class or call other functions that are not const in a function.


Inline

1) Background
The reason for inline is very similar to const. 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 C.
Macro definition in expression form.
An example of macro definition in expression form:
# Define expressionname (var1, var2) (var1 + var2) * (Var1-Var2)
      This form of expression is similar to that of a function, but it uses a pre-compiler without a stack and is more efficient than a function. However, it is only a simple replacement of the symbol table on the pre-compiler, and does not support parameter validity detection and access control by C ++ class members.
Inline's purposeTo replace the macro definition of this expression form, it eliminates its shortcomings and inherits its advantages. Inline code is efficient in the pre-compiler symbol table. It is a real function and has strict parameter detection when called. It can also be used as a member function of the class.
2) specific functions
Define function members directly in the class definition, and the system processes them as inline functions. member functions are inline functions, meaning that each object has an independent copy of the function.
Outside the class, if the keyword inline is used to define function members, the system will also process them as inline functions;


C keyword
# Define macro name code to be replaced   
Macro definition, which is saved in the pre-compiler symbol table for efficient execution. It is used as a simple symbol replacement and does not check the parameter validity.
New Type of existing type of typedef
Aliases are often used to create platform-independent types. typedef is interpreted during compilation, So it allows the compiler to replace text beyond the pre-processor capability.

Define: Define macros processed during pre-Compilation;
  Only simple character replacement, no type detection

Typedef:Define the type alias for processing complex types
 Example: typedef int;
 Then: A; // defines a as Int.

Inline:The inline function provides suggestions to the compiler. the compiler has the right to reject the macro replacement.

 Not necessarily succeeded in submitting an application

 

Static
I. background
Cause: when the program executes the variable defined in the function to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it?
The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ). This is also true for static members of the class.
Solution: Therefore, static is introduced in C ++ to modify variables, which can indicate compilation.
The variable is stored in the static storage area of the program. This achieves the goal and keeps the access range of the variable unchanged.
II. Specific functions
Static analysis summary: static always changes the storage form of variables or objects to static storage, and the connection mode to internal connections. For local variables (internal connections already exist ), it only changes the storage mode. For global variables (which are already in static storage), it only changes the connection type. (1 connection mode: internal connection; 2 storage mode: stored in the static global Storage Area)


Const
I. background
A c ++ has a strict compilation system, which allows many errors of C ++ programs to be found in the compilation phase, thus greatly reducing the error rate, it has also become an outstanding advantage in comparison with C ++.
A common preprocessing command in B C # define variablename variablevalue can be easily used for value substitution. This value substitution has at least three advantages:
The first is to avoid the appearance of numbers with vague meanings, so that the program semantics is fluent and clear, as shown in the following example:
# Define user_num_max 107 avoids the confusion caused by using 107 directly.
Second, you can easily adjust and modify parameters. In the preceding example, when the number of people changes from 107 to 201, you can modify the parameters here;
Third, it improves the execution efficiency of the program. Because the pre-compiler is used for value substitution, it does not need to allocate storage space for these constants, so the execution efficiency is high.

However, a preprocessing statement has many advantages, but it has a fatal disadvantage, that is, preprocessing
The sentence is only a replacement of simple values, and there is no type detection mechanism. In this way, the pre-processing statement cannot enjoy strict C ++
The benefits of lattice type check may become a series of errors.
The initial purpose of const is to replace precompiled commands and eliminate its shortcomings.
It inherits its advantages.
Now its form is changed:
Const datatype variablename = variablevalue;
2) specific functions
1. const is used for Pointer Analysis in two cases:
  Int const * A; // a variable, * a variable
  Int * const A; // A is not variable, * A is variable
Analysis: const is a type modifier that is left-bound. It is the same as its type Modifier on the left.
Type modifier. Therefore, int const is limited to * a, not. Int * const limits a, not *.
2. Const limits the passing value parameters of the function:
  Void fun (const int var );
  Analysis: The preceding statement specifies that parameters cannot be changed in the function body.
3. Const limits the return value of the function:
Const int fun1 ();
Const myclass fun2 ();
  Analysis: The preceding statement limits that the return value of a function cannot be updated. When the function returns an internal type (for example, fun1), it is already a value. Of course, it cannot be assigned an update, at this time, const is meaningless. It is best to remove it to avoid confusion. When the function returns a custom type (such as fun2), this type still contains variable members that can be assigned values, so it makes sense at this time.
4. Transfer and return address: This is the most common case. It can be seen from the characteristics of address variables that the appropriate use of const is significant.
5. Const qualified class member functions:
Class classname {
Public:
Int fun () const;
.....
}
Note: This post-const form is a provision to avoid confusion. Use const in the declaration and definition of this function, because const has become part of the type information.
Ability to obtain: constant objects can be operated.
Capacity Loss: you cannot modify the data members of a class or call other functions that are not const in a function.


Inline

1) Background
The reason for inline is very similar to const. 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 C.
Macro definition in expression form.
An example of macro definition in expression form:
# Define expressionname (var1, var2) (var1 + var2) * (Var1-Var2)
      This form of expression is similar to that of a function, but it uses a pre-compiler without a stack and is more efficient than a function. However, it is only a simple replacement of the symbol table on the pre-compiler, and does not support parameter validity detection and access control by C ++ class members.
Inline's purposeTo replace the macro definition of this expression form, it eliminates its shortcomings and inherits its advantages. Inline code is efficient in the pre-compiler symbol table. It is a real function and has strict parameter detection when called. It can also be used as a member function of the class.
2) specific functions
Define function members directly in the class definition, and the system processes them as inline functions. member functions are inline functions, meaning that each object has an independent copy of the function.
Outside the class, if the keyword inline is used to define function members, the system will also process them as inline functions;


C keyword
# Define macro name code to be replaced   
Macro definition, which is saved in the pre-compiler symbol table for efficient execution. It is used as a simple symbol replacement and does not check the parameter validity.
New Type of existing type of typedef
Aliases are often used to create platform-independent types. typedef is interpreted during compilation, So it allows the compiler to replace text beyond the pre-processor capability.

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.