#define与typdef在类型定义中的区别

以下的使用都是可以的:
#define idPtr int*
typedef int* itPtr;
都是可以的,但是它们还是有区别的.区别的实质所在还是 #define与typdef的含义不同.
#define macro只是简单的字符串替代.
typdef则是类型的重新定义,其所定义的类型是一体的.例如下面的语句:
idPtr n1,n2;
macro展开后是:int* n1,n2;等价于:int* n1,int n2;
itPtr n3,n2;则等价于:int* n3,int* n2;

同时下面的语句也是存在区别的.
int a[10];
const int* n1 = a;
const itPtr* n2 = a;
n1++;
n2++;//error,l-value specifies const object
其实const itPtr* n2 = a;与int* const n2 = a;等价.
posted @ 2007-05-10 14:08  fool  阅读(946)  评论(1编辑  收藏  举报