C + + typedef usage

Source: Internet
Author: User

Introduction

typedef declaration, referred to as TypeDef, creates a new name for an existing type. For example, people often use typedef to write more aesthetically pleasing and readable code. The so-called aesthetics means that typedef can hide clumsy grammatical constructs and platform-related data types, thus enhancing portability and future maintainability. The purpose of using TypeDef in programming is generally two, one is to give the variable a new name that is easy to remember and meaning, and the other is to simplify some more complex type declarations.

The typedef uses the following methods:

typedef existing_type NEW_TYPE_NAME;

Note: typedef does not create a new type. It simply adds a synonym to the existing type.

The simplest use of typedef
int WORD;

The first declaration defines a synonym for Char, with the name C, and you can use size in any context that requires an int. The second declaration defines a synonym for the unsigned int, which is named Word, and you can use Word in any context that requires an int.

typedef and arrays, pointers

Instead of repeating an array of 81-character elements as follows:

Char line[Bayi];  Char text[Bayi];  

Define a typedef that can be used whenever you want to use an array of the same type and size:

Char line[Bayi];  line text, secondline;

Similarly, you can hide the pointer syntax as follows:

char * PSTR;  "ABCint mystrcmp (PSTR, pstr);  
typedef and Functions

Function pointers are typically used for callbacks, such as signal processing, libcurl, etc., to callbacks. Callbacks are more commonly used techniques, and callbacks involve function pointers. When we have the following function in our program:

void Printhello (int i);

Then we'll define a function pointer, point to Printhello, and call this method, with the following code:

void (*pfunc) (int); pFunc = &printHello; (*pfunc) (a); 

where void (*pfunc) (int) is declaring a function pointer, pointing to the return value is void, calling the parameter is (int) The function, the variable name is Pfunc,pfunc is a function pointer, formerly is a simple use of function pointers. As you can see, declaring a function pointer is more complicated, especially if you want to declare a function pointer variable of the same type in multiple places, and the code is more complex, so here's a simplified approach:

void (*printhellohandle) (int);

Use the following code:

Printhellohandle PFunc; PFunc = &printHello; (*pfunc) (a);

In the future, programs need to declare similar function pointers, only the following code is required:

Printhellohandle Pfuncother;

In this way, our code becomes more concise and understandable.

typedef Experience

We are looking at typedef and arrays, pointers and typedef and functions, may feel more complex typedef, but in fact typedef behavior is a bit like #define macro, with its actual type instead of synonymous word. Take a look at the following example

char * PSTR;  Pstr mystr;

The code PSTR mystr, which is followed by char * mystr, replacing the mystr with the pstr of the statement typedef char * PSTR, expanded after or char * mystr. typedef does not create a new type, the typedef is interpreted at compile time, so the compiler is able to cope with text substitution beyond the preprocessor's capabilities. These rules can be applied to typedef and arrays:

Char line[81]; line text;

Replace text with a typedef char LINE[81] line, and then expand to

Char text[

can also be applied to the most complex typedef and pointers

void (*printhellohandle) (int); Printhellohandle PFunc;

Replace Pfunc with typedef void (*printhellohandle) (int), and then expand to

void (*pfunc) (int);

It is simply declaring a pfunc function pointer, there is no printhellohandle this type.

C + + typedef usage

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.