========内联函数返回值的问题,不报错==============

这不是鸭头 2010-11-14 03:51:39
以前没有注意过,今天用内联函数的时候,忘记了写返回值。VS2005竟然不报错。
如果把inline 去掉,就会报错。
加上inline 就不会报错。

inline BOOL testfun();

inline BOOL CTest::testfun()
{

}
//以上不报错。下面这样就报错。
BOOL testfun();
BOOL CTest::testfun()
{

}

这个是VS2005的bug,还是什么原因?
...全文
286 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgsdzhaolanxiang1 2010-11-14
  • 打赏
  • 举报
回复
高人。。。。。。。
信阳毛尖 2010-11-14
  • 打赏
  • 举报
回复
这个还真不清楚......
wltg2001 2010-11-14
  • 打赏
  • 举报
回复
内联函数有点类似于宏的概念,它并不是真正的函数,函数在执行时是要将指令跳转到函数入口处的,但内联函数是在编译时将它插入到源代码调用它的地方。
MoXiaoRab 2010-11-14
  • 打赏
  • 举报
回复
你没有理解Inline函数的底层原理,如果你看过编译器的逆向代码就能看到

下面引用一段大牛的话:

Using functions saves memory space because all the calls to a particular function execute the same code, which is provided only once. However, there is an overhead due to the extra time required to jump to the point that the instructions of that function are provided and then return back to the invocation point. In some cases, where very small functions are used repeatedly, it may be more efficient to have the code incorporated at the point of the function call instead of actually calling the function, so as to avoid the associated overhead. Inline functions can be used for this purpose. An inline function is a regular function with a suggestion to the compiler to insert the instructions at the points where the function is called.

A definition of a function as inline suggests to the compiler to expand the body of the function at all points where it is invoked. An inline function is expanded by the compiler during the compilation phase and not by the preprocessor (during macro substitutions). This is useful for very short functions which may result in a computational overhead whenever they are called. Inline functions eliminate the overhead of function calls while allowing the usage of procedural abstraction.

简单的说,inline函数和其他函数的不同,执行快不假,但是也有其自身的限制
此外,inline函数的声明中的函数体将被编译器展开,当执行到这个inline函数的时候,跳转过来,执行完毕函数体后,再跳转回去。

可以说,甚至不是真正意义上的函数,而是一段独立的代码。在跳转的过程中,符号就起到了很大的作用。
所以,在C++设计编译器的时候,大牛们就是这么想的,内联函数不返回值也可以
virtualcode 2010-11-14
  • 打赏
  • 举报
回复
占座。
同问

16,471

社区成员

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

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

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