码迷,mamicode.com
首页 > 其他好文 > 详细

宏定义与内置函数的比较

时间:2020-04-15 21:18:34      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:inline   缺点   style   ret   ++   line   return   bsp   lin   

/*
time:20200415
where:gfdx
man:g-7.net
*/
#include<iostream>
using namespace std;
#define doub(x)x*2
int main()
{
    for (int i = 1; i <= 4; i++)
    {
        cout << i << \t<< "doubled is" << \t<<double(i) << endl;
    }
    //计算错,编译程序解释为:cout<<"1+2doubled is<<1+2*2<<endl;
    cout << "1+2doubled is" << double(1 + 2) << endl;
    return 0;
}
/*
使用内置函数代替宏定义,就能消除宏定义的不安全性。
内置函数具有宏定义的优点但没有其缺点*/
#include<iostream>
using namespace std;
inline int doub(int x)
{
    return x * 2;
}
int main()
{
    int i;
    for (i = 1; i <= 4; i++)
    {
        cout << i <<\t<< "doubled is" <<\t<<doub(i) << endl;
    }
    cout << "1+2doubled is" <<\t<< doub(1 + 2) << endl;
    return 0;
}

 

宏定义与内置函数的比较

标签:inline   缺点   style   ret   ++   line   return   bsp   lin   

原文地址:https://www.cnblogs.com/qq1480040000/p/12708165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!