typedef usage Summary.

Source: Internet
Author: User

Reference sticker: http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

The first # define is preprocessing, and the typedef is a completely different mechanism. The detailed mechanism is not yet fully understood. Please refer to previous articles.

Usage One: pointer variable instead.

char* PA, PB; The majority does not conform to our intent, it only declares a pointer to a character variable, and a character variable

typedef char* PCHAR; Aliases are not recommended for all caps

PCHAR PA, PB; This is a pointer to two char types.

This method is considered useful in the original text. But the individual thinks this method is apt to cause misunderstanding. If you want to use it, you need to have an implied indication of the pointer in the variable name.

Usage Two: The use of struct is omitted.

struct TAGPOINT1

{
int x;

int y;
};

struct tagPOINT1 p1;

If you use TypeDef, you can:

typedef struct TAGPOINT
{
int x;

int y;
}point;

Point P1; This is less than the original way to write a struct. This method is not recommended. But you need to be able to understand this use in someone else's code.

Usage Three: use typedef to define platform-independent types. (Personal opinion of the most common usage)

Real represents the highest-precision floating-point number.

typedef long double REAL; Up to Lond double platform support

typedef double REAL;//maximum support double platform

typedef float REAL;//Highest support float platform

That is, when cross-platform, just change the typedef itself, do not make any changes to other source code.

Usage Four: Define an easy-to-understand alias for the class.

Example 1:

Class abc{/*code*/};

typedef ABC Rectangle;

You see ABC must not know what this class is for. But when you see rectangle at least you should know that this class is a rectangle.

PS: It is not recommended that anyone build ABC this kind. But you can't control everyone. So sometimes a typedef is a good choice.

Example 2:

template< class TypeA > class abc{/* code */};

typedef < CALSS AAA >ABC Rectanglea;

typedef < CALSS BBB >ABC Rectangleb;

The variable name is highly deprecated in this example. So the code for the last job. So you can only use AAA and BBB instead. But it should also be seen in the usage.

Usage Five: The alias of the function pointer.

Format: typedef return type (* new type) (parameter table)

typedef CHAR (*PTRFUN) (int);

Ptrfun Pfun;

Char getInt (int a) { return A;}

int main ()

{

Pfun = getInt;

cout<< (*pfun) (2) << Endls;

return 0;

}

This method is not used to write code. But the existence is reasonable. In essence, there is no difference from the alias of a class.

A non-experimental conjecture: Can *ptrfun be changed to Ptrfun?

Answer a question from the original: (Personal cognition alone does not mean absolutely correct.) Welcome to discuss)

Original case:

typedef char * PSTR;

Char string[4] = "ABC";

const char *P1 = string;

Const PSTR P2 = string;

p1++;

p2++;

In the original text, it is pointed out that p2++ will go wrong.

1:PSTR is a custom type, and the preceding const will make it impossible for us to change the value of this type.

2: The inverse inference, this pstr is saved is a pointer. The specific content is the pointer address. When the pointer address is + +, the specific content is changed. So the error.

typedef usage Summary.

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.