c ++内联函数? [英] c++ inline function?

查看:100
本文介绍了c ++内联函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为什么要这样做:

inline double square (double x) { return x*x;}

而不是

double square (double x) { return x*x;}

推荐答案

前者(使用 inline 函数在头文件中,它可以包含在多个源文件中。使用 inline 文件范围中创建标识符,很像声明 static 。如果不使用 inline ,则会从链接器获取多个符号定义错误。

The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.

当然,除了对编译器的提示,函数应该被编译为 inline 到使用它(避免函数调用开销)。编译器不需要对 inline 提示执行操作。

Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.

这篇关于c ++内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆