您的位置:首页 > 其它

typedef与#define的区别

2016-03-21 21:04 204 查看

1. typedef

typedef故名思意就是类型定义的意思,但是它并不是定义一个新的类型而是给已有的类型起一个别名,在这一点上与引用的含义类似,引用是变量或对象的别名,而typedef定义的是类型的别名。typedef的作用主要有两点:


1.1
简化复杂的类型声明

简化复杂的类型声明,或给已有类型起一含义明确的别名;如:
typedef
bool (*FuncPointer)(int,
double);
//声明了一个返回 bool 类型并带有两个(int和double)形参的函数的指针类型FuncPointer
FuncPointer pFunc;
//声明了一个FuncPointer类型的函数指针对象pFunc


1.2
定义与平台无关的类型

定义与平台无关的类型,屏蔽不同平台的类型差异化;如:

用typedef来定义与平台无关的类型。

比如定义一个叫
REAL 的浮点类型,在目标平台一上,让它表示最高精度的类型为:
typedef
long
double REAL;

在不支持
long double 的平台二上,改为:
typedef
double REAL;

在连
double 都不支持的平台三上,改为:
typedef
float REAL;

也就是说,当跨平台时,只要改下
typedef 本身就行,不用对其他源码做任何修改。

标准库就广泛使用了这个技巧,比如size_t。另外,因为typedef是定义了一种类型的新别名,不是简单的字符串替换,所以它比宏来得稳健。


1.3
与struct的结合使用

在C++中,struct与class的作用相同,就是默认的访问权限不同,struct默认为public,而class默认为private的。

【例1.3.1】:

[cpp]
view plain
copy
print?





struct Person
{
string name;
int age;
float height;

};
Person person;

struct Person
{
string name;
int age;
float height;
};
Person person;


定义一个Struct的类型Person,定义一个Person的对象person。

或者

[cpp]
view plain
copy
print?





struct Person
{
string name;
int age;
float height;

}person;

struct Person
{
string name;
int age;
float height;
}person;


定义一个Struct的类型Person,在定义的同时还声明了一个Person的对象person。

但是在C语言中,struct的定义和声明要用typedef。

【例1.3.2】:

[cpp]
view plain
copy
print?





typedef struct __Person

{
string name;
int age;
float height;

}Person; //这是Person是结构体的一个别名

Person person;

typedef struct __Person
{
string name;
int age;
float height;
}Person;	//这是Person是结构体的一个别名
Person person;


如果没有typedef就必须用struct
Person person;来声明,如:

【例1.3.3】

[cpp]
view plain
copy
print?





struct Person
{
string name;
int age;
float height;

};
struct Person person;

struct Person
{
string name;
int age;
float height;
};
struct Person person;




[cpp]
view plain
copy
print?





struct Person
{
string name;
int age;
float height;

}person; //person是Person的对象

struct Person
{
string name;
int age;
float height;
}person;	//person是Person的对象


2. typedef与#define的区别


2.1.
执行时间不同

关键字typedef在编译阶段有效,由于是在编译阶段,因此typedef有类型检查的功能。

#define则是宏定义,发生在预处理阶段,也就是编译之前,它只进行简单而机械的字符串替换,而不进行任何检查。

【例2.1.1】typedef会做相应的类型检查:

[cpp]
view plain
copy
print?





typedef unsigned
int UINT;

void func()
{
UINT value =
"abc"; // error C2440: 'initializing' : cannot convert from 'const char [4]' to 'UINT'

cout << value << endl;
}

typedef unsigned int UINT;

void func()
{
UINT value = "abc";	// error C2440: 'initializing' : cannot convert from 'const char [4]' to 'UINT'
cout << value << endl;
}


【例2.1.2】#define不做类型检查:

[cpp]
view plain
copy
print?





//#define用法例子:
#define f(x) x*x
int main()
{
int a=6, b=2, c;

c=f(a) / f(b);
printf("%d\n", c);

return 0;
}

//#define用法例子:
#define f(x) x*x
int main()
{
int a=6, b=2, c;
c=f(a) / f(b);
printf("%d\n", c);
return 0;
}


程序的输出结果是: 36,根本原因就在于#define只是简单的字符串替换。


2.2.
功能有差异

typedef用来定义类型的别名,定义与平台无关的数据类型,与struct的结合使用等。

#define不只是可以为类型取别名,还可以定义常量、变量、编译开关等。


2.3.
作用域不同

#define没有作用域的限制,只要是之前预定义过的宏,在以后的程序中都可以使用。

而typedef有自己的作用域。

【例2.3.1】没有作用域的限制,只要是之前预定义过就可以

[cpp]
view plain
copy
print?





void func1()
{
#define HW "HelloWorld";

}

void func2()
{
string str = HW;
cout << str << endl;
}

void func1()
{
#define HW "HelloWorld";
}

void func2()
{
string str = HW;
cout << str << endl;
}


【例2.3.2】而typedef有自己的作用域

[cpp]
view plain
copy
print?





void func1()
{
typedef unsigned
int UINT;

}

void func2()
{
UINT uValue = 5;//error C2065: 'UINT' : undeclared identifier

}

void func1()
{
typedef unsigned int UINT;
}

void func2()
{
UINT uValue = 5;//error C2065: 'UINT' : undeclared identifier
}


【例2.3.3】

[cpp]
view plain
copy
print?





class A
{
typedef unsigned
int UINT;

UINT valueA;
A() : valueA(0){}
};

class B
{
UINT valueB;
//error C2146: syntax error : missing ';' before identifier 'valueB'

//error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

};

class A
{
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};

class B
{
UINT valueB;
//error C2146: syntax error : missing ';' before identifier 'valueB'
//error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
};


上面例子在B类中使用UINT会出错,因为UINT只在类A的作用域中。此外,在类中用typedef定义的类型别名还具有相应的访问权限,【例2.3.4】:

[cpp]
view plain
copy
print?





class A
{
typedef unsigned
int UINT;

UINT valueA;
A() : valueA(0){}
};

void func3()
{
A::UINT i = 1;
// error C2248: 'A::UINT' : cannot access private typedef declared in class 'A'

}

class A
{
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};

void func3()
{
A::UINT i = 1;
// error C2248: 'A::UINT' : cannot access private typedef declared in class 'A'
}


而给UINT加上public访问权限后,则可编译通过。

【例2.3.5】:

[cpp]
view plain
copy
print?





class A
{
public:
typedef unsigned int
UINT;
UINT valueA;

A() : valueA(0){}
};

void func3()
{
A::UINT i = 1;

cout << i << endl;
}

class A
{
public:
typedef unsigned int UINT;
UINT valueA;
A() : valueA(0){}
};

void func3()
{
A::UINT i = 1;
cout << i << endl;
}



2.4.
对指针的操作

二者修饰指针类型时,作用不同。

[cpp]
view plain
copy
print?





typedef int * pint;

#define PINT int *

int i1 = 1, i2 = 2;

const pint p1 = &i1; //p不可更改,p指向的内容可以更改,相当于 int * const p;

const PINT p2 = &i2;
//p可以更改,p指向的内容不能更改,相当于 const int *p;或 int const *p;

pint s1, s2; //s1和s2都是int型指针

PINT s3, s4; //相当于int * s3,s4;只有一个是指针。

void TestPointer()
{
cout << "p1:" << p1 << " *p1:" << *p1 << endl;

//p1 = &i2; //error C3892: 'p1' : you cannot assign to a variable that is const

*p1 = 5;
cout << "p1:" << p1 <<
" *p1:" << *p1 << endl;

cout << "p2:" << p2 <<
" *p2:" << *p2 << endl;
//*p2 = 10; //error C3892: 'p2' : you cannot assign to a variable that is const

p2 = &i1;
cout << "p2:" << p2 << " *p2:" << *p2 << endl;

}

typedef int * pint;
#define PINT int *

int i1 = 1, i2 = 2;

const pint p1 = &i1;	//p不可更改,p指向的内容可以更改,相当于 int * const p;
const PINT p2 = &i2;	//p可以更改,p指向的内容不能更改,相当于 const int *p;或 int const *p;

pint s1, s2;	//s1和s2都是int型指针
PINT s3, s4;	//相当于int * s3,s4;只有一个是指针。

void TestPointer()
{
cout << "p1:" << p1 << "  *p1:" << *p1 << endl;
//p1 = &i2;	//error C3892: 'p1' : you cannot assign to a variable that is const
*p1 = 5;
cout << "p1:" << p1 << "  *p1:" << *p1 << endl;

cout << "p2:" << p2 << "  *p2:" << *p2 << endl;
//*p2 = 10;	//error C3892: 'p2' : you cannot assign to a variable that is const
p2 = &i1;
cout << "p2:" << p2 << "  *p2:" << *p2 << endl;
}


结果:

p1:00EFD094 *p1:1

p1:00EFD094 *p1:5

p2:00EFD098 *p2:2

p2:00EFD094 *p2:5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: