前几天参加某公司的笔试,有一道题是用纯C实现一个泛型函数。郁闷了好久用C++模板实现了。
宏有如下的特点:
1.与const相比,宏是在预编译的时候完成的
2.define 只做简单的替换,不做类型安全检查
3.使用不当会引起很多问题
宏的用法:
1.简单的宏定义
#define MAX 1024
2.宏定义功能块
#define MAX(a,b) (a)>(b)?(a):(b) //比较大小的宏
3定义函数实现泛型
4.定义类
#define _DECL_class(name, element )/
class df_##nm/
{/
protected:/
element *pData;/
int num;/
/
void _resize(int n);/
/
public:/
nm( ){};/
/
int Count() const { return num; }/
/};
_DECL_class( cls_example, int );