Typedef declaration, or typedef for short, creates a new name for an existing type. For example, t ypedef is often used to write more beautiful and readable code. The so-called beauty means that typedef can hide clumsy Syntax structures and platform-related data types to enhance portability and future maintainability. Typedef is used most often to create a type name that is easy to remember and use it to archive the programmer's intent. Type appears in the declared variable name, which is on the right of the ''typedef ''keyword. Example: typedef int size;
This statement defines an int synonym with the name size. Note that typedef does not create a new type. It only adds a synonym for an existing type. You can use size in any context that requires an int.
Typedef can also conceal conforming types, such as pointers and arrays. For example, you do not need to repeatedly define an array with 81 characters as follows:
Charline [81]; char text [81];
Define a typedef. When an array of the same type and size is used, the following can be used: typedef char Line [81]; Line text, secondline;
Another important purpose of typedef is to define machine-independent types. For example, you can define a floating point type called REAL. On the target machine, it can obtain the highest precision:
Typedef long double REAL;
On a machine that does not support long double, the typedef looks like the following: typedef double REAL;
In addition, on a machine that does not support double, the typedef looks like this: typedef float REAL;
You do not need to make any changes to the source code, you can compile this REAL-type application on each platform. The only change is typedef itself. In most cases, even this tiny change can be automatically implemented through fantastic Conditional compilation. Isn't it? The standard library uses typedef to create such platform-independent types: size_t, ptrdiff, and fpos_t. In addition, typedef, such as st d: string and std: ofstream, also hides long, incomprehensible template-specific syntax, such as: basic_string <char, char_traits <char>, allocator <cha r> and basic_ofstream <char, char_traits <char>. Note 1: note the difference between typedef and define.
Typdef int * a, B
Then a and B are of the same type.
Define int * a, B
The a and B types are different. Note 2:
Typedef int (* mac_listen_func) (int *, char *); mac_listen_func mlf1, mlf2;
Equivalent Definition:
Int (* mlf1) (int *, char *); int (* mlf1) (int *, char *);
The above method is easy to use and easier to read.
Typedef declaration, or typedef for short, creates a new name for an existing type. For example, typedef is often used to write more beautiful and readable code. The so-called beauty means that typedef can hide clumsy Syntax structures and platform-related data types to enhance portability and future maintainability. This article will do its best to reveal the powerful features of typedef and how to avoid some common traps.
How to create platform-independent data types and hide clumsy and incomprehensible syntaxes?
Use typedefs to create a synonym for an existing type.
Defining a type name that is easy to remember typedef is most commonly used to create a type name that is easy to remember and use it to archive programmer's intent. Type appears in the declared variable name, which is on the right of the ''typedef ''keyword. For example:
Typedef int size;
This statement defines an int synonym with the name size. Note that typedef does not create a new type. It only adds a synonym for an existing type. You can use
Size:
Void measure (size * psz );
Size array [4];
Size len = file. getlength ();
Std: vector <size>;
Typedef can also conceal conforming types, such as pointers and arrays. For example, you do not need to repeatedly define an array with 81 characters as follows:
Char line [81];
Char text [81];
Define a typedef. Whenever an array of the same type and size is used, you can do this:
Typedef char Line [81];
Line text, secondline;
Getline (text );
Similarly, you can hide the pointer syntax as follows:
Typedef char * pstr;
Int mystrcmp (pstr, pstr );
This will take us to the first typedef trap. The standard function strcmp () has two 'constch ar * 'parameters. Therefore, it may mislead people to declare mystrcmp () as follows ():
Int mystrcmp (const pstr, const pstr );
This is incorrect. In order, 'constpstr' is interpreted as 'Char * const' (a constant pointer pointing to cha r ), instead of 'const char * '(pointer to constant char ). This problem can be easily solved:
Typedef const char * cpstr;
Int mystrcmp (cpstr, cpstr); // It is correct now
Remember: Whenever typedef is declared for the pointer, a const must be added to the final typedef name so that the pointer itself is a constant rather than an object.
Simplified code
The typedef behavior discussed above is a bit like the # define macro, which replaces the synonym with its actual type. The difference is that typedef is interpreted during compilation, so let the compiler handle text replacement beyond the pre-processor capability. For example:
Typedef int (* PF) (const char *, const char *);
This statement introduces the PF type as the synonym of the function pointer. This function has two parameters of the const char * type and a return value of the int type. If you want to use the following form of function declaration
It is indispensable to describe this typedef:
PF Register (PF pf );
The Register () parameter is a PF type callback function that returns the address of a function with the same signature as the previously registered name. Take a deep breath. The following shows how to implement the declaration without typedef:
Int (* Register (int (* pf) (const char *, const char *)))
(Const char *, const char *);
Few programmers understand what it means, not to mention the risk of errors caused by such obscure code. Obviously, using typedef is not a kind of privilege, but a necessity. Skeptical people may ask, "OK, will someone write such code ?", Quickly browse the header file that reveals the signal () function <cs inal>, a function with the same interface.
Typedef and storageclass specifier are not surprising. typedef is like auto, extern, mutable, stat ic, just like register. It is a storage keyword. This means that typedef will really affect the Storage Feature of objects. It only means that in the statement structure, the typedef declaration looks like a variable declaration of static, extern, and other types. The following is a second trap:
Typedef register int FAST_COUNTER; // Error
Compilation and translation fail. The problem is that you cannot have multiple storage-class keywords in the Declaration. Because the symbol typedef occupies the storage class keyword, register (or any other storage class keyword) cannot be used in the typedef declaration ).
Promote cross-platform development
Another important purpose of typedef is to define machine-independent types. For example, you can define a floating point type called REAL. On the target machine, it can obtain the highest precision:
Typedef
Long double REAL;
Not Supported
On the long double machine, the typedef looks like the following:
Typedef
Double REAL;
In addition, on a machine that does not support double, the typedef looks like this :,
Typedef float REAL;
You do not need to make any changes to the source code, you can compile this REAL-type application on each platform. The only change is typedef itself. In most cases, even this tiny change can be automatically implemented through fantastic Conditional compilation. Isn't it? The standard library uses typedef to create such platform-independent types: size_t, ptrdiff, and fpos_t. In addition, typedef such as std: string and std: ofstream also hides long, incomprehensible template-specific syntax, such as: basic_string <char, char_traits <char>, allocator <char> and basic_ofstream <char, char_traits <char>.