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 One, creating 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 variable defined in this function is not 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
second, specific role
Const
One, 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.
b C is a very common preprocessor directive #define VARIABLENAME VariableValue can easily be substituted for values, This value substitution has the advantage of at least three advantages:
#define USER_NUM_MAX 107 This avoids the confusion of using 107 directly.
II is easy to adjust and modify the parameters, as in the above example, when the number from 107 to 201, change here;
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;
2) Specific role
1.const Two-case analysis for pointers:
int const *A; A variable, *a immutable
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 between an inline function and a macro is that a macro is replaced with a macro by a preprocessor, and inline functions are implemented through compiler control. and the inline function is the real function, but when needed, the inline function expands like a macro, so it cancels the function's argument stack and reduces the cost of the call. You can call inline functions just like you would call a function, without worrying about some of the problems that might arise from dealing with macros. We can use inline to define inline functions, but any function defined in the Description section of the class is automatically considered an inline function.
Let's take a look at the use of inline functions.
The inline function must be declared together with the function body to be valid. A declaration like this is that inline tablefunction (int I) is ineffective, and the compiler simply declares the function as a normal function, and we must define the function body.
Inline tablefunction (int I) {return i*i};
So we can define an inline function. We can call it the same as a normal function. However, the execution speed is faster than the normal function.
we can also define functions that are defined outside the class as inline functions , such as:
Class tableclass{
Private:
Int i,j;
Public:
Int Add () {return i+j;};
Inline int Dec () {return i-j;}
Int Getnum ();
}
inline int Tableclass::getnum () {
return I;
}
The three functions declared above are inline functions. In C + +, a function that defines the body of a function within a class is implicitly considered an inline function. Regardless of whether you have the inline keyword.
How do I tell the compiler to make a member function an inline function?
Declaring an inline member function looks very similar to a normal function:
Class Fred {public:
void f (int i, char c);};
But when you define an inline member function, precede the member function definition with the inline keyword, and put the definition in the header file: inlinevoid fred::f (int i, char c) {//...} Typically the definition of a function ({...} is mandatory in the header file. If you place the definition of an inline function in a. cpp file and call it in a different. cpp file, the connector will give a "unresolved external" error.
Inline functions are most widely used in C + + classes and should be used to define access functions. The classes we define generally define data members 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 define these read-write member functions as inline functions, we will get better efficiency.
Class sample{
Private:
Int ntest;
Public:
Int Readtest () {return ntest;}
Void settest (int I) {ntest=i;}
}
Of course, the inline function also has some limitations. The execution code in the function is not too much, if the function body of the inline function is too large, the general compiler will discard the inline mode and call the function in a normal way. In this way, the inline function is as efficient as the normal function execution.
c keyword
#define The code to replace the macro name
Macro definition, saved in the pre-compiler symbol table, perform efficiently; as a simple symbol substitution, no detection of the validity of the parameters
typedef has a new type of type
aliases, commonly used to create platform-independent types, are interpreted at compile-time, so that the compiler can cope with text substitution beyond the preprocessor's capabilities
Define and inline