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可以簡化操作符,或使用自己熟悉的操作符來代替不熟悉的。
新聞熱點
疑難解答