C + + Inline

Source: Internet
Author: User

One: inline definition and use

The inline keyword is used to define an inline function of a class, and the main reason for introducing it is to replace the macro definition in the form of an expression in C.

An example of a macro definition in the form of an expression:

#define EXPRESSIONNAME (VAR1,VAR2) ((VAR1) + (VAR2)) * ((VAR1)-(VAR2)) Why replace this form, and listen to my word: 1. First of all, the reason for using this form of macro definition in C, C language is a very efficient language, this macro definition in form and use like a function, but it uses the preprocessor implementation, without the parameters of the stack, code generation, and so a series of operations, therefore, the efficiency is very high, this is its use in C is a major reason.2. This macro definition is similar in form to a function, but when it is used, it simply replaces the preprocessor symbol table, so it cannot detect the validity of the parameters, nor can it enjoy C + +CompilerThe advantage of strict type checking, in addition its return value can not be cast to the appropriate type convertible, so that its use there is a series of hidden dangers and limitations. 3. Access control for classes and classes is introduced in C + +, so that if an operation or an expression involves a protected member or private member of a class, you cannot use the macro definition to implement it (because the this pointer cannot be placed in the appropriate location). 4. 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. Second,Why is inline a good alternative to pre-defined?

Corresponds to the above 1-3 points, elaborated as follows:

1. inline functions of the class defined by the line, the code of the function is placed in the symbol table and replaced directly when used (like a macro), without the overhead of the call, and the efficiency is high.

2. Obviously, the inline function of a class is also a true function, and when the compiler calls an inline function, it first checks the type of its arguments to ensure that the call is correct. Then perform a series of related checks, just like any real function. This eliminates its hidden dangers and limitations.

3. Inline can be used as amember functions of the class, you can, of course, use the protected members and private members of the class in which they are located. when to use the inline function:First, you can use the inline function to completely replace the macro definition in the form of an expression. also note that inline functions are generally only used when the function is very simple , because the code of the inline function expands wherever it is called, and if the function is too complex, the consequences of code bloat are likely to outweigh the benefits of increased efficiency. The most important place to use the inline function is the access function for the class. three how to use the inline function of a class:

Simply mention the use of inline:

1. Define this function in the class:

Class classname{

.....   ....

INT getwidth () {return m_lpicwidth;}; If defined directly in the class, no inline decoration is required, and the compiler automates the inline function

....//This statement is mentioned in "C + + Primer"

....

}

2. Add the inline keyword before the out-of-class definition:

Class Account {

Public

Account (double initial_balance) {balance = initial_balance;}//Same as 1

Double GetBalance (); Declaring in a class

Double Deposit (double Amount);

Double withdraw (double Amount);

Private

Double balance;

};

Inline double account::getbalance () {return balance;}//Add the inline keyword when you define out-of-class

Inline double account::D eposit (double Amount) {return (balance + = Amount);}

Inline double Account::withdraw (double Amount) {return (balance-= Amount);}

There are also some rules to be aware of:

1, the inline description is only a suggestion to the compiler, the compiler can choose to ignore this recommendation. For example, if you specify a function of more than 1000 lines as inline, the compiler ignores the inline and restores the function to a normal function.

2, in calling the inline function, to ensure that the definition of the inline function to "see" the compiler, that is, the definition of the inline function in the header file , which is not the same as the usual function definition. But if you're used to putting a function definition in a CPP file, or if you want to make the header file a little more concise, you can do this:

In SomeInline.h

#ifndef Someinline_h

#define Someinline_h

inline Type Example (void);

//........ Declarations of other functions

#include "SomeInlie.cpp"//source file suffix name varies by compiler

#endif

In SomeInline.cpp

#include "SomeInline.h"

Type Example (void)

{

//..........

}

//............... Definition of other functions

The above method is universal, valid, and can be used with confidence, do not have to worry about the header files containing CPP files will lead to compilation errors.

Note the point:

Inline functions can eliminate the efficiency burden of function calls and preserve the advantages of general functions. However, the inline function is not a panacea, and in some cases it can even reduce the performance of the program. So be cautious when you use it.
1. Let's take a look at the benefits of the inline function: from a user's point of view, the inline function looks like a normal function, it can have parameters and return values, it can have its own scope, but it does not introduce the burden of general function calls. In addition, it can be more secure and easier to debug than macros.
Of course, it should be realized that inline specifier is only a suggestion to the compiler, and the compiler has the right to ignore this suggestion. So how does the compiler decide whether the function is inline or not? In general, the key factors include the size of the function body, whether the local object is declared, the complexity of the function, and so on.
2. So what happens if a function is declared inline but not inline? Theoretically, when the compiler refuses to inline a function, that function is treated like a normal function, but there are some other problems.

It is not advisable to use inline in the following situations:

(1) If the code in the function body is longer, using inline will result in a high cost of memory consumption.

(2) If there is a loop in the function body, the time to execute the function body code is greater than the cost of the function call. The constructors and destructors of a class are easily misunderstood to be more efficient for inline use. Be aware that constructors and destructors may hide behaviors such as "secretly" executing constructors and destructors for base or member objects. So don't arbitrarily place the definitions of constructors and destructors in class declarations. A good compiler will automatically cancel the unworthy inline based on the body of the function definition (this further illustrates that inline should not appear in the declaration of the function).

Reference: http://blog.csdn.net/zhangchao3322218/article/details/8099747

Http://www.cnblogs.com/berry/articles/1582702.html

C + + Inline

Related Article

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.