C + + keyword inline

Source: Internet
Author: User

In c&c++, 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)) supersedes this form for the following reasons: 1. The reason for this form of macro definition using define in C is because C is a very efficient language, which is defined as a function in form and use, but it is implemented using a preprocessor, without parameter stack, code generationAnd so on a series of operations, so the efficiency is very high, this is it in C is used in a major reason. 2. This macro definition is similar in form to a function, but when it is used, it is simply a simple substitution in the preprocessor symbol table, so it cannot detect the validity of the parameters, nor can it enjoy the benefits of strict type checking by the C + + compiler, and its return value cannot be cast to the appropriate type of convertible, so that Its use has 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 inline rollout is also to replace the macro definition in this form of expression, which eliminates the disadvantages of macro definitions and inherits the benefits of macro definitions well.Pre-definedCorresponds 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 a member function of a class, and of course you can use the protected members and private members of the class in which it resides. 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.How to useSimply mention the use of inline: 1. Define this function in the class: Class classname{... INT getwidth () {return m_lpicwidth;}; If you define directly in a class and do not need inline adornments, the compiler automates the inline function ....//This statement is mentioned in the C + + Primer ....} 2. Add the inline keyword before the out-of-class definition:
123456789101112 classAccount {public:Account(doubleinitial_balance) { balance = initial_balance; } //与1相同doubleGetBalance(); //在类中声明doubleDeposit( doubleAmount );doubleWithdraw( doubleAmount );private:doublebalance;};inlinedoubleAccount::GetBalance() { returnbalance; } //在类外定义时添加inline关键字inlinedoubleAccount::Deposit( double Amount ) { return( balance += Amount ); }inlinedoubleAccount::Withdraw( doubleAmount ) { return( balance -= Amount ); }
In addition, there are some rules to note: 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:
1 //SomeInline.h中
12345678910111213 #ifndef  someinline_h #define  SOMEINLINE_H inline  type example ( void //... Declaration of other functions #include "SomeInlie.cpp"  //source file suffix name varies by compiler #endif //someinline.cpp in # 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.

C + + keyword 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.