1// typedef [type] [alias]
2// Example:
3typedef unsigned long int ulong;
4
5ulong someNumber = 158426;
1#include <iostream>
2 int main(){
3 typedef unsigned int ui;
4 ui i = 5, j = 8;
5 std::cout << "i = " << i << std::endl;
6 std::cout << "j = " << j << std::endl;
7 return 0;
8}
9
1// simple typedef
2typedef unsigned long ulong;
3
4// the following two objects have the same type
5unsigned long l1;
6ulong l2;
7
8// more complicated typedef
9typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
10
11// the following two objects have the same type
12int a1[10];
13arr_t a2;
14
15// common C idiom to avoid having to write "struct S"
16typedef struct {int a; int b;} S, *pS;
17
18// the following two objects have the same type
19pS ps1;
20S* ps2;
21
22// error: storage-class-specifier cannot appear in a typedef declaration
23// typedef static unsigned int uint;
24
25// typedef can be used anywhere in the decl-specifier-seq
26long unsigned typedef int long ullong;
27// more conventionally spelled "typedef unsigned long long int ullong;"
28
29// std::add_const, like many other metafunctions, use member typedefs
30template< class T>
31struct add_const {
32 typedef const T type;
33};
34
35typedef struct Node {
36 struct listNode* next; // declares a new (incomplete) struct type named listNode
37} listNode; // error: conflicts with the previously declared struct name