关键字-inline内联函数

1.为什么使用内联函数及其原理

 作用:消除函数调用开销,用于优化规模小、流程直接、频繁调用的函数。

 理解:将内联函数在其调用点“内联的”展开。

 注意:内联函数只是向编译器发出一个请求,编译器在进行编译时是可以忽略这个请求的

//定义内联函数
inline const string& shorterString(const string &s1, const string &s2)
{
     return s1.size() <= s2.size() ? s1 : s2;
}

//使用内联函数
cout << shorterString(s1, s2) << endl;
//上一句的等效表达
//cout << (s1.size() < s2.size() ? s1 : s2) << endl;
2.类中如何使用内联函数

 分为显示定义和隐式定义两种,对于显示定义,也可以在类内声明之后定义时声明为

class Screen {
public:
    char get() const { return contents[cursor]; }  //隐式内联       
    inline char get(pos ht, pos wd) const;         // 显式内联
    Screen &move(pos r, pos c);      	           //在其后声明为内联
};

inline  Screen &Screen::move(pos r, pos c)
{
    return *this;        // return this object as an lvalue
}






 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值