当前位置:K88软件开发文章中心编程语言C/C++C/C++01 → 文章内容

c++中typedef的使用方法

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:07:02

:2010-04-29 10:57:00

c++中有一个关键字typedef,它主要是把一种数据类型定义为另一个标识符来使用,在程序中使用该标识符来实现相应数据类型变量的定义。下面给出三种常用的地方。

(1)简单类型替换:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef int INT;

int main( ){
 INT  a;
 a = 10;
 //a = "a";//false
 cout<<a;
}

(2)定义数组类型:

#include "stdafx.h"  
#include <iostream>   
#include <string>  
using namespace std;  
 
typedef int A[3];  
int main(){  
    A b = {3,4,5};  
    cout<<sizeof(b);  
}

(3)定义函数指针

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

typedef void (*F)(int);
void print1(int x){
 cout<<x;
}
int main(){
 F a;
 a = print1;
 (*a)(20);
}

从上面的例子可以看出,有时typedef可以简化操作符,或使用自己熟悉的操作符来代替不熟悉的。



c++中typedef的使用方法