Лекция: Creating New Data Type Names

It is possible in C++ for a programmer to create additional names for existing data types. Creating another name uses the keyword typedef. The syntax to create a new name is as follows.

type-expressionnew-name

Example 1 Usage of typedef

The following listing contains a few examples of the use of the keyword typedef.

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: #include <iostream>#include <cstdlib> using namespace std; typedef int my_int;typedef my_int* my_int_ptr; int main(int argc, char* argv[]) { my_int i = 10; my_int_ptr ptr = &i; cout << *ptr << endl; return EXIT_SUCCESS;}
Listing 8 A sample use of typedef

Using data type names created with typedef allows programmers to encapsulate their choice of data types. This is good programming practice, since, if the need arises, a programmer can easily change all uses of a particular data type simply by changing the definition of the typedef. Additional benefits of the use of typedef will be examined in 1.5.3 Templates.

еще рефераты
Еще работы по информатике