📄 [9] inline functions, c++ faq lite.htm
字号:
href="http://www.sunistudio.com/cppfaq/ctors.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[9.4]></A>
<DIV class=FaqTitle>
<H3>[9.4] 如何告诉编译器使非成员函数成为内联函数?</H3></DIV>
<P>声明内联函数看上去和普通函数非常相似:
<P>
<DIV class=CodeBlock><TT> void f(int i, char c);
</TT></DIV>
<P>当你定义一个内联函数时,在函数定义前加上 <TT>inline</TT> 关键字,并且将定义放入头文件:
<P>
<DIV
class=CodeBlock><TT> inline<BR> void f(int i, char c)<BR> {<BR> </TT><EM>// ...</EM><TT><BR> }
</TT></DIV>
<P>注意:将函数的定义(<TT>{</TT>...<TT>}</TT>之间的部分)放在头文件中是强制的,除非该函数仅仅被单个 .cpp 文件使用。尤其是,如果你将内联函数的定义放在 .cpp 文件中并且在其他 .cpp文件中调用它,连接器将给出
“unresolved external” 错误。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/ctors.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[9.5]></A>
<DIV class=FaqTitle>
<H3>[9.5] 如何告诉编译器使一个成员函数成为内联函数?</H3></DIV>
<P>声明内联成员函数看上去和普通函数非常类似:
<P>
<DIV
class=CodeBlock><TT> class Fred {<BR> public:<BR> void f(int i, char c);<BR> };
</TT></DIV>
<P>但是当你定义内联成员函数时,在成员函数定义前加上 inline 关键字,并且将定义放入头文件中:
<P>
<DIV
class=CodeBlock><TT> inline<BR> void Fred::f(int i, char c)<BR> {<BR> </TT><EM>// ...</EM><TT><BR> }
</TT></DIV>
<P>通常将函数的定义(<TT>{</TT>...<TT>}</TT>之间的部分)放在头文件中是强制的。如果你将内联函数的定义放在 .cpp 文件中并且在其他 .cpp 文件中调用它,连接器将给出“unresolved external”错误。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/ctors.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[9.6]></A>
<DIV class=FaqTitle>
<H3>[9.6] 有其它方法告诉编译器使成员函数成为内联吗?</H3></DIV>
<P>有:在类体内定义成员函数:
<P>
<DIV
class=CodeBlock><TT> class Fred {<BR> public:<BR> void f(int i, char c)<BR> {<BR> </TT><EM>// ...</EM><TT><BR> }<BR> };
</TT></DIV>
<P>尽管这对于写类的人来说很容易,但由于它将类是“什么”(what)和类“如何”(how)工作混在一起,给阅读的人带来了困难。我们通常更愿意<A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#[9.5]">在类体外使用 <TT>inline</TT> 关键字</A>定义成员函数来避免这种混合。这种感觉所基于的认识是:在一个面向重用的世界中,使用你的类的人有很多,而建造它的人只有一个(你自己);因此你做任何事都应该照顾多数而不是少数。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/ctors.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[9.7]></A>
<DIV class=FaqTitle>
<H3>[9.7] 内联函数保证执行性能更好吗? <IMG alt=UPDATED!
src="[9] Inline functions, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently
explained "code bloat" and also added lots of if's, and's and but's (on 4/01).
<A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.5]">Click here to go to
the next FAQ in the "chain" of recent
changes<!--rawtext:[10.5]:rawtext--></A>.]</EM></SMALL>
<P>不。
<P>小心过度使用内联函数可能导致代码膨胀。在页面调度环境中,它可能会给执行性能带来负面影响。
<P>代码膨胀术语只表示代码的尺寸会增大(膨胀)。在有关内联函数的上下文中,更关心的是内联函数会增加执行代码的尺寸,并导致操作系统不稳定。这意味着操作系统要花费大部分的时间从磁盘取出代码。<BR><BR>当然,内联函数也可能减小执行代码的尺寸。看上去反了,其实是真的。特别是,调用函数的代码总量有时会大于展开的内联函数的代码总量。这样的情况会发生于非常小的函数,当优化器能删除很多冗余代码时——也就是当优化器能使长的函数变短时,也可能会发生于长的函数。<BR><BR>因此结论就是:没有简单的定论。你必须因地制宜。不要使得答案象这样的单纯化,“不要用内联函数”或“总是使用内联函数”或“当且仅当函数代码少于
N 行时用内联函数”。这种一刀切的方法可能用起来非常简单,但是它们产生的并不是最佳结果。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/references.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/ctors.html">Next section</A>
]</SMALL>
<HR>
<P><A name=bottom></A><A href="mailto:cline@parashift.com"><IMG height=26
alt=E-Mail src="[9] Inline functions, C++ FAQ Lite.files/mbox.gif"
width=89> E-mail the author</A><BR>[ <A
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>
| <A
href="http://www.sunistudio.com/cppfaq/index.html#table-of-contents">Table of contents</A>
| <A
href="http://www.sunistudio.com/cppfaq/subject-index.html">Subject index</A>
| <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.1]">About the author</A>
| <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">©</A>
| <A
href="http://www.sunistudio.com/cppfaq/on-line-availability.html#[2.2]">Download your own copy</A> ]<BR><SMALL>Revised
Apr 8, 2001</SMALL> </P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -