Use typedef to suppress inferior code

xiaoxiao2021-03-05  52

Author: Danny Kalev

Compilation: MTT studio

Original source: Using TypedEf to Curb Miscreant Code

Summary: TypeDef declaration helps to create a platform-independent type, and even hide complicated and difficult to understand syntax. In any case, use TypeDef to bring unexpected benefits for the code. Through this article, you can learn Typedef to avoid lack of lack, thus make the code more robust.

TypeDef declaration, referred to as Typedef, create a new name for existing types. For example, people often use TypedEf to write a more beautiful and readable code. The so-called beautiful, means that typedef can hide clumsy syntax structure and platform-related data types, enhanced portability and future maintenanceability. This article will do our utmost to reveal the TypedEf power and how to avoid some common traps. How to create a platform-independent data type, hide the clumsy and understanding syntax? Use typedefs to create a synonym for existing types. Defining the type name of easy memory type TypeDef uses the most place to create a type name that is easy to remember, use it to archive programmers. The type appears in the declared variable name, located on the right side of the '' typef '' key. E.g:

Typedef int size;

This statement defines an int synonym, named size. Note Typedef does not create a new type. It only adds a synonym to an existing type. You can use Size in any context that requires Int:

Void measure (size * psz); size array [4]; size len = file.getLength (); std :: vector vs;

TypedEf can also cover the type, such as a pointer and an array. For example, you don't have to repeat an array of 81 character elements like this:

Char line [81]; char text [81];

Define a TypedEf, whenever you want to use the same type and size, you can do this:

Typedef char line [81]; line text, secondline; getLine (text);

Similarly, you can hide the pointer syntax like this:

Typedef char * pstr; int mystrcmp (PSTR, PSTR);

Here will take us to the first TypeDef trap. Standard Functions Strcmp () has two 'const char *' types of parameters. Therefore, it may mislead people like this to declare mYStrCMP ():

Int MyStrCMP (Const PSTR, Const PSTR);

This is wrong, in order, 'const pstr' is interpreted as 'char * const' (a constant pointer to char), not 'const char *' (pointing to a pointer to constant char). This problem is easy to resolve:

Typedef const char * cpstr; int mystrcmp (cpstr, cpstr); // is now correct

Remember: When, as long as you declare Typedef for your pointer, you must add a const in the final TypeDef name so that the pointer itself is constant, not an object. The code simplifies the TypeDef behavior discussed above is a bit like #define macros, with its actual type to replace synonymous words. Different points are typedef to be interpreted at compile, so let the compiler to deal with text replacements that exceed the pre-processor capability. For example: typedef int (* pf) (const char *, const char *);

This declaration introduces the PF type as the synonym of the function pointer, which has two const char * type parameters and an Int type return value. If you want to declare the following forms, then the above typefef is indispensable:

PF register (PF PF);

The parameter of the register () is a PF type callback function that returns an address of a function, and its signature is the same as the previously registered name. Do a deep breath. Below I show, if you don't have to use TypeDef, how we do this:

Int (* Register (const char *, const char *)) (const char *, const char *);

Few of the programmers understand what it means, not to mention the error risk caused by this puzzle code. Obviously, this use typedef is not a privilege, but a required. People who have skeptical attitudes may ask: "OK, will someone still write such code?", Quickly browse the header file of the signal () function, a function of the same interface. Typedef and Storage Class Specifier This statement is a bit surprising. Typedef is like AUTO, EXTERN, Mutable, Static, and Register, is a storage class keyword. This is to say Typedef truly affect the storage characteristics of the object; it is just on the statement composition, the TypeDef declaration looks like static, extern, etc. variable declaration. The following will be taken to the second trap:

TypedEf register int found_counter; // error

Compiled. The problem is in that you can't have multiple storage class keywords in the statement. Because the symbol TypedEf has occupied the location of the storage class key, the register (or any other storage class key) cannot be used in the TypeDef declaration. Promote cross-platform development TypedEF has another important purpose, that is, define the type of machine-independent type, for example, you can define a floating point type called REAL, which can I get the highest precision on the target machine:

Typedef long double real;

On the machine that does not support Long Double, this typedef looks like this:

Typedef double real;

And, on the machine that does not support Double, this typedef looks like this:,

Typedef float real;

转载请注明原文地址:https://www.9cbs.com/read-32958.html

New Post(0)