Author: bigcoder, http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html
Whether in C or C ++ code, the word typedef is widely used. Of course, the frequent occurrence is still in C code. Typedef is similar to # define, but it is more different, especially in some complicated usage, I read some blogs of C/C ++ learners on the Internet. One of the blogs about typedef is still very good. Because of the excellent summary, I will not add any modified references, the following is the referenced content (the red part is the content written by the author ).
Purpose 1:
Define a type of Alias, not just a simple macro replacement. It can be used as multiple objects that declare the pointer type at the same time. For example:
Char * pA, PB; // most of them do not conform to our intention. It only declares a pointer to a character variable,
// And a character variable;
The following are feasible:
typedef char* PCHAR;PCHAR pa, pb;
This usage is very useful, especially the definition of char * pa and Pb. Beginners often think that two character-type pointers are defined. Actually, this is not the case. Using typedef char * pchar won't happen, reduces errors.
Purpose 2:
It is used in the old C code to help struct. In the previous Code, when declaring a new struct object, you must carry the struct, that is, the form is: struct structure name object name, such:
struct tagPOINT1 { int x; int y;};struct tagPOINT1 p1;
In C ++, you can directly write: Structure name object name, that is, tagpoint1 P1;
typedef struct tagPOINT{ int x; int y;}POINT;POINT p1;
// In this way, a struct is written less than the original method, which is easier to use, especially when a large amount of data is used.
Mr. Maybe, in C ++, this type of purpose 2 of typedef is not very big, but I understand it and grasp the old generation
Code is still helpful. After all, we may encounter code left over from earlier years in the project.
Purpose 3:
Use typedef to define platform-independent types.
For example, define a floating point type called real. On the target platform 1, make it the highest precision type:
Typedef long double real;
On Platform 2 that does not support long double, change:
Typedef double real;
On Platform 3 that is not supported by double, change:
Typedef float real;
That is to say, when using a cross-platform system, you only need to change the typedef itself, without any modifications to other source codes.
This technique is widely used in the standard library, such as size_t. In addition, because typedef defines a new type of Alias, rather than a simple string replacement, it is more robust than a macro.
This advantage can reduce the amount of Code while writing code!
Purpose 4:
Define a new simple alias for complex statements. The method is: gradually replace one with an alias in the original statement.
The statement is divided into complex statements. In this loop, the part with the variable name is reserved for the final replacement, and the original declaration is the most simplified.
. Example:
Original Declaration: void (* B [10]) (void (*)());
The variable name is B. Replace pfunparam with alias 1 in the brackets on the right:
Typedef void (* pfunparam )();
Replace B with the variable on the left, and pfunx with alias 2:
Typedef void (* pfunx) (pfunparam );
The most simplified version of the original statement:
Pfunx B [10];
Original statement: Doube (*) (* E) [9];
The variable name is E. Replace the left part with pfuny as Alias 1:
Typedef double (* pfuny )();
Replace the variable E on the right, and pfunparamy is alias 2.
Typedef pfuny (* pfunparamy) [9];
The most simplified version of the original statement:
Pfunparamy E;
Understand the "right-left rule" available for complex statements: Starting from the variable name, the variable name must first go to the right, and then to the left. A parentheses
The reading direction is changed. After the analysis in the brackets is complete, the brackets are displayed, or in the order of right and left.
The entire statement is analyzed. Example:
INT (* func) (int * P );
First, find the variable name func. There is a pair of parentheses and a * sign on the left. This indicates that func is a pointer.
And then jump out of the parentheses, first look at the right side, and then encounter parentheses, which means (* func) is a function, so
Func is a pointer to this type of function, that is, a function pointer. This type of function has an int * type parameter and returns a value.
The type is int.
INT (* func [5]) (int *);
The right side of func is a [] Operator, indicating that func is an array with five elements; the left side of func is *, indicating
The element of func is a pointer (note that * is not modifying func, but modifying func [5], because the [] Operator
The priority is higher than *, and func is first combined ). Jump out of the brackets and look at the right side. The parentheses indicate the number of func.
The element of the group is a pointer of the function type. It points to a function that has an int * type parameter, and the return value type is int.
This kind of usage is complicated, and there are many occurrences of this kind of usage, which is often not understandable. I believe the above explanation can be helpful.
* ***** The above is a reference part, and the following is my understanding section *****
Example:
1. Comparison 1:
# Include <iostream> using namespace STD; typedef int (* A) (char, char); int SS (char a, char B) {cout <"function 1" <Endl; cout <A <Endl; cout <B <Endl; return 0 ;}int BB (char, char B) {cout <"function 2" <Endl; cout <B <Endl; cout <A <Endl; return 0;} void main () {A; A = SS; A ('A', 'B'); A = BB; A ('A', 'B ');}
2. Comparison 2:
typedef int (A) (char, char);void main(){ A *a; a = ss; a('a','b'); a = bb; a('a','b');}
The results of both programs are the same:
Function 1
A
B
Function 2
B
A
* ***** The following is a reference section *****
Reference: http://blog.hc360.com/portal/personShowArticle.do? ArticleID = 57527
Differences between typedef and # define:
Case 1:
Generally, typedef is better than # define, especially when there is a pointer. See the example below:
Typedef char * pstr1;
# Define pstr2 char *;
Pstr1 S1, S2;
Pstr2 S3, S4;
In the preceding variable definition, S1, S2, and S3 are both defined as char *, while S4 is defined as char, not ours.
The root cause of the expected pointer variable is that # define is a simple string replacement, while typedef is
Type.
Case 2:
In the following code, the compiler reports an error. Do you know which statement is wrong?
Typedef char * pstr;
Char string [4] = "ABC ";
Const char * P1 = string;
Const pstr P2 = string;
P1 ++;
P2 ++;
P2 ++ error. Another reminder is that typedef is different from # define, which is not simple.
Text replacement. In the above Code, const pstr P2 is not equal to const char * P2. Const pstr P2 and
Const long X has no difference in nature. It only imposes read-only restrictions on variables, except for the data class of the variable P2.
Type is defined by ourselves rather than the inherent type of the system. Therefore, the meaning of const pstr P2 is: limited number
The P2 variable whose data type is char * is read-only, so P2 ++ is incorrect. Although the author has already explained it clearly here, I am still confused in this place. I really hope some experts can help me give some advice, in particular, this sentence "The data type of variable P2 here is defined by ourselves rather than the inherent type of the system." is it true that the data type defined by myself is first modified by const, the change operation cannot be executed, but the type defined by the system can be?