- ·上一篇内容:相对路径与绝对路径
- ·下一篇内容:C++中抛出0xc0000417异常错误代码的原因
c++中typedef的使用方法
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可以简化操作符,或使用自己熟悉的操作符来代替不熟悉的。
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论