关于内联函数

项目管理PMO 2002-03-21 11:30:40
在两种情况下,Visual C++按正常方式创建内联函数的独立副本并以函数方式而不是内联方式调用它:递归函数调用和在转换单元其它地方引用的函数。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
请问这句话是什么意思?
...全文
50 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cct8 2002-03-21
  • 打赏
  • 举报
回复
其实很简单,内联函数某些方面相当于MACRO,有些方面类似于const。
就如const,compiler尽量不分配空间存放一个const,除非发现程序中有什么地方用到它的地址。如果程序中什么地方需要一个内联函数的地址(这两种情况其实都包括在这里)则compiler必须创建该函数的“独立副本”,以便其他地方引用。递归情况下,如果没有函数地址,将导致程序代码大小为无穷。

翻译的确实不好。
qfpcn 2002-03-21
  • 打赏
  • 举报
回复
这里把“递归函数”理解成一种转换单元,而且,我想这里的“递归函数”指的也只是“直接递归函数”,而把“非直接”的递归函数归为“转换单元其它地方引用的函数”。。。
个人见解,仅作参考
niu_a 2002-03-21
  • 打赏
  • 举报
回复
The inline specifier instructs the compiler to replace function calls with the code of the function body. This substitution is “inline expansion” (sometimes called “inlining”). Inline expansion alleviates the function-call overhead at the potential cost of larger code size.

The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are:

Recursive functions.


Functions that are referred to through a pointer elsewhere in the translation unit.
///////////////////////////////////////////
C and C++ programs consist of one or more source files, each of which contains some of the text of the program. A source file, together with its include files (files that are included using the #include preprocessor directive) but not including sections of code removed by conditional-compilation directives such as #if, is called a “translation unit.”
先解释translation unit,应该称为翻译单元,翻译是编译器在编译之前所作的一个操作,(讲宏替换,加入include的内容,去掉注释等等)一般来说就是一个.c 或者.cpp文件。
inline函数在下列情况下不被替换
1、递归调用
2、在别的翻译单元被通过函数指针调用
ndy_w 2002-03-21
  • 打赏
  • 举报
回复
谁翻译的?
项目管理PMO 2002-03-21
  • 打赏
  • 举报
回复
我是说“在转换单元其它地方引用的函数”是指什么情况
项目管理PMO 2002-03-21
  • 打赏
  • 举报
回复
谢谢几位的回答,我把帖子加了分,以表示感谢。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧