⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 [9] inline functions, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
href="http://www.sunistudio.com/cppfaq/ctors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[9.4]></A>
<DIV class=FaqTitle>
<H3>[9.4] 如何告诉编译器使非成员函数成为内联函数?</H3></DIV>
<P>声明内联函数看上去和普通函数非常相似: 
<P>
<DIV class=CodeBlock><TT>&nbsp;void&nbsp;f(int&nbsp;i,&nbsp;char&nbsp;c); 
</TT></DIV>
<P>当你定义一个内联函数时,在函数定义前加上&nbsp;<TT>inline</TT>&nbsp;关键字,并且将定义放入头文件: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;inline<BR>&nbsp;void&nbsp;f(int&nbsp;i,&nbsp;char&nbsp;c)<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;} 
</TT></DIV>
<P>注意:将函数的定义(<TT>{</TT>...<TT>}</TT>之间的部分)放在头文件中是强制的,除非该函数仅仅被单个&nbsp;.cpp&nbsp;文件使用。尤其是,如果你将内联函数的定义放在&nbsp;.cpp&nbsp;文件中并且在其他&nbsp;.cpp文件中调用它,连接器将给出 
“unresolved&nbsp;external” 错误。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[9.5]></A>
<DIV class=FaqTitle>
<H3>[9.5] 如何告诉编译器使一个成员函数成为内联函数?</H3></DIV>
<P>声明内联成员函数看上去和普通函数非常类似: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Fred&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;void&nbsp;f(int&nbsp;i,&nbsp;char&nbsp;c);<BR>&nbsp;}; 
</TT></DIV>
<P>但是当你定义内联成员函数时,在成员函数定义前加上&nbsp;inline&nbsp;关键字,并且将定义放入头文件中: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;inline<BR>&nbsp;void&nbsp;Fred::f(int&nbsp;i,&nbsp;char&nbsp;c)<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;} 
</TT></DIV>
<P>通常将函数的定义(<TT>{</TT>...<TT>}</TT>之间的部分)放在头文件中是强制的。如果你将内联函数的定义放在&nbsp;.cpp&nbsp;文件中并且在其他&nbsp;.cpp&nbsp;文件中调用它,连接器将给出“unresolved&nbsp;external”错误。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[9.6]></A>
<DIV class=FaqTitle>
<H3>[9.6] 有其它方法告诉编译器使成员函数成为内联吗?</H3></DIV>
<P>有:在类体内定义成员函数: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Fred&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;void&nbsp;f(int&nbsp;i,&nbsp;char&nbsp;c)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;}; 
</TT></DIV>
<P>尽管这对于写类的人来说很容易,但由于它将类是“什么”(what)和类“如何”(how)工作混在一起,给阅读的人带来了困难。我们通常更愿意<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#[9.5]">在类体外使用&nbsp;<TT>inline</TT>&nbsp;关键字</A>定义成员函数来避免这种混合。这种感觉所基于的认识是:在一个面向重用的世界中,使用你的类的人有很多,而建造它的人只有一个(你自己);因此你做任何事都应该照顾多数而不是少数。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html">Next&nbsp;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>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html">Next&nbsp;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>&nbsp;E-mail the author</A><BR>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html#table-of-contents">Table&nbsp;of&nbsp;contents</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/subject-index.html">Subject&nbsp;index</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.1]">About&nbsp;the&nbsp;author</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">&copy;</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/on-line-availability.html#[2.2]">Download&nbsp;your&nbsp;own&nbsp;copy</A>&nbsp;]<BR><SMALL>Revised 
Apr 8, 2001</SMALL> </P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -