suppressing inferior code with typedef

Source: Internet
Author: User
Tags constant int size

Summary: TYPEDEF declarations help create platform-independent types and even hide complex and incomprehensible syntax. In any case, using a typedef can bring unexpected benefits to your code, and you can learn to use typedef to avoid imperfections, making your code more robust.

A typedef declaration, called a TypeDef, creates a new name for an existing type. For example, people often use a typedef to write more beautiful and readable code. The so-called aesthetics, meaning that the typedef can hide clumsy syntax constructs and platform-related data types, thereby enhancing portability and future maintainability. This article will make every effort to uncover the powerful features of the typedef and how to avoid some common pitfalls.

How do I create a platform-independent data type that hides awkward and incomprehensible syntax?

Use typedefs to create synonyms for an existing type.

Define type names that are easy to remember

The most common use of a typedef is to create a type name that is easy to remember, and use it to archive the programmer's intent. The type is now declared in the variable name, located on the right side of the ' typedef ' keyword. For example:

typedef int size;

This declaration defines a synonym for int, with a name of size. Note that a typedef does not create a new type. It simply 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 <size> vs;

A typedef can also mask conforming types, such as pointers and arrays. For example, you don't have to repeatedly define an array of 81 character elements as follows:

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

Defines a typedef that you can do whenever you want to use an array of the same type and size:

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 parameters for the ' const char * ' type. Therefore, it may mislead people to declare mystrcmp () as follows:

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 * ' (a pointer to a constant char). This problem is easy to solve:

typedef const char * cpstr;
int mystrcmp(cpstr, cpstr); // 现在是 正确的

Remember: Whenever you declare a typedef for a pointer, add a const to the final typedef name so that the pointer itself is a constant, not an object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.