现在的位置: 首页 > 综合 > 正文

走进C++程序世界—-宏定义和内联函数及模板说明

2014年01月30日 ⁄ 综合 ⁄ 共 2948字 ⁄ 字号 评论关闭

1. 简单宏定义

 
 
简单的宏定义有如下格式:
 
[#define指令(简单的宏)]  #define  标识符替换列表
 
替换列表是一系列的C语言记号,包括标识符、关键字、数、字符常量、字符串字面量、运算符和标点符号。当预处理器遇到一个宏定义时,会做一个 “标识符”代表“替换列表”的记录。在文件后面的内容中,不管标识符在任何位置出现,预处理器都会用替换列表代替它。
使用#define来为常量命名有许多显著的优点:
1) 、 程序会更易读。
2) 、 程序会更易于修改。我们仅需要改变一个宏定义,就可以改变整个程序中出现的所有该常量的值。
3) 、可以帮助避免前后不一致或键盘输入错误。
4) 、可以对C语法做小的修改。
看下面的代码
/*
 *macr0_expansion.cpp
 *cDate : 2013-11-16
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
using namespace std;

/*在宏中使用括号
 *宏与函数及模板之比较
 * 1、宏在每次被使用时都按内联方式展开
 * 2、速度比函数调用快,没有函数调用的开销
 * 3、宏不能出现在编译器使用的中间源代码中,调试器无法看到,宏调试比较棘手
 * 4、宏不是类型安全的,宏几乎可以提供任何类型的参数
 * */

#define CUBE(a) ((a)*(a)*(a))
#define THREE(a) a*a*a

int main()
{
    long x = 5;

    cout <<" CUBE(" << x  <<"): " << CUBE(x) << endl;
    cout <<" THREE(" << x  <<"): " << THREE(x) << endl;

    long a = 5,b= 7;

    /* (5 + 7)*(5 + 7)*(5 + 7) */
    cout <<" CUBE(5 + 7): " << CUBE(a + b) << endl;
    /* 5 + 7 *5 + 7*5 + 7 */
    cout <<" THREE(5  + 7): " << THREE(a + b) << endl;

    return 0;
}

内联函数:

可以把内联看成是一种特殊形式的宏 如一段代码很小但是被频繁调用就可以考虑把他声明为inline(或者__foeceinline)的 它主要从运行速度来考虑 不一定会减小程序大小 甚至可能更大 写成inline不一定真的inline 编译器会看情况选择是否真的inline 当调用这个函数相当于一个宏一样的时候 由于省掉了函数调用开销 运行速度就会变得更快

/*
 *inline.cpp
 *cDate : 2013-11-16
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
using namespace std;

/* 被声明为内联,像宏一样每次调用时将就地展开
 *没有函数调用的开销。
 * */
inline unsigned long square(unsigned long a)
{
    return a*a;
}

inline unsigned long cube(unsigned long a)
{
    return a*a*a;
}

int main()
{
    unsigned long x;
    for(;;){
        cout << "请输入一个无符号整型(输入0时,退出):" << endl;
        cin >> x;

        if(x == 0){
            break;
        }

        cout << " you entered : " << x<<endl;
        cout << "\t square(" << x << "):\t" << square(x) <<endl;
        cout << "\t cube(" << x << "):\t" << cube(x) <<endl;

    }
    return 0;
}    

模板使用,

/*
 *template_class.cpp
 *cDate : 2013-9-28
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
using namespace std;

template <typename T1=int,typename T2=double>
class CholdPair
{
private:
    T1 m_Value1;
    T2 m_Value2;
public:
    CholdPair(const T1 & value1,const T2 & value2)
    {
        m_Value1 = value1;
        m_Value2 = value2;
    }

    const T1 & getFristValue()
    {
        return m_Value1;
    }

    const T2 & getSecondValue()
    {
        return m_Value2;
    }
};

int main()
{
    CholdPair <> mIntFloatPair(111,10.09);
    CholdPair <short,char *> mShortStringPair(25,"I am a good boy!");

    cout << "Value1: " <<mIntFloatPair.getFristValue() <<endl;
    cout << "Value2: " <<mIntFloatPair.getSecondValue() <<endl;

    cout << "Value1: " <<mShortStringPair.getFristValue() <<endl;
    cout << "Value2: " <<mShortStringPair.getSecondValue() <<endl;
    
    return 0;
}


[root@localhost operation]# cat template_clas.cpp 


/*
 *template_class.cpp
 *cDate : 2013-9-28
 *Author: sjin
 *Mail:413977243@qq.com
 */
#include <iostream>
using namespace std;

template <typename T1=int,typename T2=double>
class CholdPair
{
private:
    T1 m_Value1;
    T2 m_Value2;
public:
    CholdPair(const T1 & value1,const T2 & value2)
    {
        m_Value1 = value1;
        m_Value2 = value2;
    }

    const T1 & getFristValue()
    {
        return m_Value1;
    }

    const T2 & getSecondValue()
    {
        return m_Value2;
    }
};

int main()
{
    //模板实例化
    CholdPair <> mIntFloatPair(111,10.09);
    CholdPair <short,char *> mShortStringPair(25,"I am a good boy!");

    cout << "Value1: " <<mIntFloatPair.getFristValue() <<endl;
    cout << "Value2: " <<mIntFloatPair.getSecondValue() <<endl;

    cout << "Value1: " <<mShortStringPair.getFristValue() <<endl;
    cout << "Value2: " <<mShortStringPair.getSecondValue() <<endl;
    
    return 0;
}

输出

Value1: 111
Value2: 10.09
Value1: 25
Value2: I am a good boy!

抱歉!评论已关闭.