Using Typedef With Type Casts
A typedef is created using type declaration syntax but can be used as if it were created using type cast syntax. (Type casting changes a data type.) For instance, in each line after the first line of:
typedef int (*funcptr)(double); // pointer to function of double returning int funcptr x = (funcptr) NULL; // C or C++ funcptr y = funcptr(NULL); // C or C++ funcptr z = static_castfuncptr
is used on the left-hand side to declare a variable and is used on the right-hand side to cast a value. Thus, typedefs can be used by programmers who do not wish to figure out how to convert declaration syntax to type cast syntax.
Note that, without the typedef, it is generally not possible to use declaration syntax and cast syntax interchangeably. For example:
void *p = NULL; int (*x)(double) = (int (*)(double)) p; // This is legal int (*)(double) y = (int (*)(double)) p; // Left-hand side is not legal int (*z)(double) = (int (*p)(double)); // Right-hand side is not legalRead more about this topic: Typedef
Famous quotes containing the words type and/or casts:
“Under the species of Syndicalism and Fascism there appears for the first time in Europe a type of man who does not want to give reasons or to be right, but simply shows himself resolved to impose his opinions.”
—José Ortega Y Gasset (18831955)
“When will the veil be lifted that casts so black a night over the universe? God of Israel, lift at last the gloom: For how long will you be hidden?”
—Jean Racine (16391699)