c++中有一個(gè)關(guān)鍵字typedef,它主要是把一種數(shù)據(jù)類型定義為另一個(gè)標(biāo)識符來使用,在程序中使用該標(biāo)識符來實(shí)現(xiàn)相應(yīng)數(shù)據(jù)類型變量的定義。下面給出三種常用的地方。
(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)定義數(shù)組類型:
#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)定義函數(shù)指針
#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);
}
從上面的例子可以看出,有時(shí)typedef可以簡化操作符,或使用自己熟悉的操作符來代替不熟悉的。
新聞熱點(diǎn)
疑難解答
圖片精選