`
swincle
  • 浏览: 76858 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Inline Functions

    博客分类:
  • C++
 
阅读更多

如果频繁地调用很少语句的小函数,这些开销对性能的影响不好说。所以需要Inline Functions(内联函数),例如:

#include<iostream>

using namespace std;

inline bool isNumber(char);  //Inline Functions

int main() {
    char c;
    while(cin >> c && c != '\n') {
        if(isNumber(c))
            cout << "you entered a digit.\n";
        else
            cout << "you entered a non-digit.\n";
    }
}

bool isNumber(char ch) {
    return ch >= '0' && ch <= '9';
}

 

内联函数使用的场合一般为:

(1)函数体适当小,这样就使嵌入工作容易进行,不会破坏原调用主体。一般适用于只有1~5行的小函数,且不能含有复杂的结构控制语句,如switch和while。

(2)程序中特别是在循环中反复执行该函数,这样就使嵌入的效率相对较高。例如上述例子中在循环里调用函数会影响性能,而用内联函数会提高性能。

(3)程序并不多处出现该函数调用,这样就使嵌入工作量相对较少,代码量也不会剧增。

分享到:
评论

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...

    Think in C++ 英文版(含卷一、卷二)

    9: Inline Functions 10: Name Control 11: References & the Copy-Constructor 12: Operator Overloading 13: Dynamic Object Creation 14: Inheritance & Composition 15: Polymorphism & Virtual Functions 16: ...

    Linux内核设计与实现-第三版-Robert love

    Linux内核设计与实现-第三版 Table of Contents ...Inline Functions 18 Inline Assembly 19 Branch Annotation 19 No Memory Protection 20 No (Easy) Use of Floating Point 20 Small, Fixed-Size Stack 20

    ISO/IEC C 标准第二版(2003)

    In addition to the facilities provided by C, C provides additional data types, classes, templates, exceptions, namespaces, inline functions, operator overloading, function name overloading, ...

    Prentice.Hall.C++.for.Business.Programming.2nd.Edition.2005.chm

    Special Topics: Friends, Operator Overloading, Macros, and Inline Functions 730 Section 15.1. friend Functions 731 Section 15.2. Overloading Basic Arithmetic Operators 738 Section 15.3. ...

    深度探索模C++对象模型PDF

    4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,and Copy) 纯虚拟函数的存在(Presence of a Pure ...

    深度探索C++对象模型 超清版

    4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,and Copy) 纯虚拟函数的存在(Presence of a Pure ...

    Practical C++ Programming C++编程实践

    Inline Functions Versus Normal Functions Case Study: Optimizing a Color-Rendering Algorithm Programming Exercises Answers to Chapter Questions 18. Operator Overloading Creating a Simple Fixed-Point ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,andCopy) 纯虚拟函数的存在(Presence of a Pure ...

    native_buffer.rar_return

    Definitions of non-inline functions _Base_bitset.not found, so return an indication of failure.

    Net开源日志组件

    layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that ...

    WTL 9.1 5270 ReadMe 中文 汉化 中英文对照版 01d

    The WTL classes are mostly templated and use minimal instance data and inline functions. They were not designed as a framework, so they do not force a particular application model, and can ...

    Debugging with GDB --2007年

    Table of Contents Summary of GDB . . . . . . . . ....Free software ....Contributors to GDB ....A Sample GDB Session ....Loading the Executable ....Setting Display width....Setting Breakpoints ....Running the executable ...

    WTL 9.1 5270 Beta 中文 汉化 中英文对照版

    The WTL classes are mostly templated and use minimal instance data and inline functions. They were not designed as a framework, so they do not force a particular application model, and can ...

    WizFlow网页编辑

    (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the...

    hibernate-shards.jar

    (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the...

    Universal-USB-Installer

    安装linux工具源码 (UUI) Universal USB Installer ?...This Open Source tool falls under the GNU General Public License Version 2 Source Code is made available at time of download, from the official UUI ...

    最新版的DebuggingWithGDB7.05-2010

    Table of Contents Summary of gdb . . . . . . . . ....Free Software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

    cSharpCollectionFundamentals:Pluralsight C#集合基础课程的源代码-Source code collection

    这些示例使用C#6和7中的可用功能,例如String Interpolation和Inline Functions 。 这些示例还尝试使用一种C#编程的功能样式,而不是命令式的样式。 函数样式的常见示例是在for和foreach类的循环结构上使用LINQ...

    matlab导入excel代码-wi18-matlab-workshop:Matlab2018冬季研讨会

    matlab导入excel代码Matlab研讨会 指令视窗 命令窗口可立即响应测试代码。 数学课程最有可能希望您构建函数并在命令窗口中运行它们,以便逐步学习方法。 一些有用的命令窗口工具是: ...functions to c

Global site tag (gtag.js) - Google Analytics