C language connector '/' use C ++ inline function (Inline) and macro definition # define

Source: Internet
Author: User

(1)

"/" And a macro with a long number of rows
The macro definition can contain more than two lines of commands. In this case, you must add "/" on the rightmost side and the line "/". No more characters are allowed, and no comments are allowed, the following line must end with "/", "/" followed by a space, and an error will be reported, not to mention it.
# Define Exchange (a, B ){/
Int t ;/
T = ;/
A = B ;/
B = T ;/
}

(2)

'/' And a long condition judgment statement

# Define maxa_z 20;

Int A, B, C, D, E, F, G;

If (A> = maxa_z) | (B> = maxa_z) | (C> = maxa_z) | (D> = maxa_z) | (E> = maxa_z) | (F> = maxa_z) & (G> = maxa_z ))

{

....

}

 

The judgment statement is

If (A> = maxa_z) | (B> = maxa_z) | (C> = maxa_z) |/

| (D> = maxa_z) | (E> = maxa_z) | (F> = maxa_z) & (G> = maxa_z ))

{

....

}

The following conversions are incorrect.

If (A> = maxa_z) | (B> = maxa_z) | (C> = maxa_z) |/

| (D> = maxa_z) | (E> = maxa_z) | (F> = maxa_z) & (G> = maxa_z ))

{

....

}

The second row of the judgment statement must be written in the top level.

If the connector is inserted in the following position, the second line does not need to be fixed, but the first line "/" cannot have any characters, comments and spaces.

If (A> = maxa_z) | (B> = maxa_z) | (C> = maxa_z )/

| (D> = maxa_z) | (E> = maxa_z) | (F> = maxa_z) & (G> = maxa_z ))

{

....

}

 

 

Considerations for inline functions:

1. inlined functions generally do not contain loop statements and switch statements.

2. Definitions of inline functions must appear before the first call.

3. Do not declare abnormal interfaces for inline functions

If any of the above considerations is violated, the compiler will ignore the existence of the keyword inline and process it like a common function without generating an extended code.

 

An inline function is a function that inserts a code into the caller's code. Like the # define macro, inline functions improve execution efficiency by avoiding the overhead of calls, especially when they can be optimized by the compiler by calling ("procedural integration.
Inline functions are similar to macros, but the difference is that macros are replaced by pre-processors, while inline functions are implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues.
Declaring an inline function looks very similar to a common function:
Void F (int I, char C );


When you define an inline function, add the inline keyword before the function definition and put the definition into the header file:
Inline
Void F (int I, char C)
{
//...
}


Any function defined in the description section of the class will be automatically considered as an inline function.
Inline functions are valid only when they are declared together with the function body. Declarations such as inline tablefunction (int I) have no effect. The Compiler just uses the function as a general function declaration. We must define the function body.
Inline tablefunction (int I) {return I * I };


This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions.
We can also define external functions defined in the class as inline functions, such:
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;
}


All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword.
Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.
Class sample {
PRIVATE:
Int ntest;
Public:
Int readtest () {return ntest ;}
Void settest (int I) {ntest = I ;}
}


Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.

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.