Inline functions
Problems: (Page 372)
There are two problems with the use of proprocessor macros in C++. The first is also with C: a macro looks like a function call, but does not act like one. This can bury difficult-to-find bugs. The second problem is specific to C++: the preprocessor has no permission to access class member data. This means preprocessor macros cannot be used as class member functions.
There are many subtle bugs examples with MACRO in Page 372.
Inline functions:
Any function defined within a class body is automatically inline, but you can also make a non-class function inline by preceding it with the inline keyword. You must include the function body with the declaration, otherwise the compiler will treat it as an ordinar function declaration.
Inline function implementation mechanism: (Page 377)
You will almost want to put inline definitions in a header file. When the compiler sees such a definition, it puts the function type and the function body in its symbol table. When you use the function, the compiler checks to ensure the call is correct and the return value is being used correctly, and then substitutes the function body for the function call, thus eliminating the overhead. The inline code does occupy space, but if the function is small, this can actually take less space than the code generated to do an ordinary function call.
The following rules of thumb might help:
inline function must be small;
access function can be inline function.
Limitations: (Page 390)
There are two situations in which the compiler cannot perform inlining.
Firstyly, the compiler cannot perform inlining if the function is too complicated.
Secondly, the complier also cannot perform inlining if the address of the function is taken implicitly or explicitly.
Three special features in the C preprocessor:
stringizing #define DEBUG(x) cout << #x " = " << x << endl
string concatenation
token pasting
#define FIELD(a) char* a##_string; in a##_size
class Record{
FIELD(one);
FIELD(two);
FIELD(three);
};
Inline functions的更多相关文章
- why inline functions must be put in header files?
[why inline functions must be put in header files?] 编译中有2个过程:compile.link.先进行compile,compile中把源代码编译成 ...
- C++对象模型——Inline Functions(第四章)
4.5 Inline Functions 以下是Point class 的一个加法运算符的可能实现内容: class Point { friend Point operator+(const Poin ...
- 内联函数(Inline Functions)
影响性能的一个重要因素是内联技巧.内联函数也可称为内嵌函数. 在C++中,函数调用需要建立栈环境,进行参数复制,保护调用现场,返回时,还要进行返回值复制,恢复调用现场.这些工作都是与完成特定任务的操作 ...
- The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
https://stackoverflow.com/questions/30045871/sorting-the-view-based-on-frequency-in-sql-server Just ...
- C++ inline weak symbol and so on
关于inline这个关键字,听到强调得最多的是,它只是一种对于编译器的建议,而非强制执行的限定. 但事实上,即使这个优化最终由于函数太过复杂的原因没有达成,加上inline关键字(还有在类定义中直接定 ...
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
- (转) Functions
Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...
- About Why Inline Member Function Should Defined in The Header File
About why inline member function should defined in the header file. It is legal to specify inline on ...
- 什么是内联函数(inline function)
In C, we have used Macro function an optimized technique used by compiler to reduce the execution ti ...
随机推荐
- TDD中的迭代
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:TDD中的迭代.
- 【面试虐菜】—— Oracle中CHAR、VARCHAR的区别
1.CHAR. CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间,不足的自动用空格填充,所以在读取 ...
- 淘宝HSF服务的原理以及简单的实现
淘宝HSF服务具体来说分三个应用:api接口,service服务,本地应用. 最基本的Api服务应该是十分干净的,不含方法,只有接口.它是要被打包(jar包的形式)到中央仓库去的. service服务 ...
- 设置background属性使用selector的时候内置?attr报错的解决方案
当我们设置background属性的时候可以设置background="@color/black" 也可以设置 background="@drawable/selecto ...
- Java Inner class
2016-03-27 内部类:就是把一个类的定义放在另外一个外围类定义的里面. class OutterClass{ class InnerClass { } } 1. 内部类主要有四种:成员内部类( ...
- Eclipse SVN插件账号、密码修改
操作系统:win7 svn插件:Window -> Preferences -> Team -> SVN 修改方式: 1,删除C:\Users\用户名\AppData\Roaming ...
- zTree判断是否为父节点
var treeObj = $.fn.zTree.getZTreeObj("tree"); var nodes = treeObj.getSelectedNodes(); if(t ...
- Python之路【第二十四篇】:Python学习路径及练手项目合集
Python学习路径及练手项目合集 Wayne Shi· 2 个月前 参照:https://zhuanlan.zhihu.com/p/23561159 更多文章欢迎关注专栏:学习编程. 本系列Py ...
- ffmpeg之移植到ARM
移植方法分为两种:第一种手工移植,第二种buildroot移植. 第一种手工移植: 优点:灵活性高 缺点:重复工作多 一.配置 ./configure --enable-memalign-hack - ...
- My97DatePicker 没有权限问题
引自:http://blog.sina.com.cn/s/blog_4b7809800100wkv4.html 今天遇到了My97DatePicker在不同IE版本中使用时有时出现没有权限错误的问题, ...