内联函数vs #define [英] inline function vs #define

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

问题描述


可能重复:

内联函数与预处理器宏

在C ++中,内联函数限定符基本上将该函数替换为#define伪指令,而不是一遍又一遍地调用该函数。因此,减少开销时间,但同时增加程序大小。

In C++ the inline function qualifier basically replaces the function as a #define directive, rather than making the function being called over and over again. Thus reducing overhead time, but at the same time increasing program size.

如果我对内联函数的理解是正确的,inline和#define有什么不同? / p>

If my understanding of inline function is correct, what is the different, of inline and #define?

推荐答案


  • inline 编译器可以忽略的提示

    • inline is only a hint, which the compiler is free to ignore

      #define


      • 为了在宏中安全地放置多个语句,必须将它包装在 do {...} while(0)

      • 每次使用宏都会重新计算宏的参数。 / li>
      • 呼叫者范围中的姓名可以访问此

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

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