Home>

Foreword

An identifier in C++ is accompanied by various modifiers,Makes the original meaning of the identifier less intuitive,It even needs careful analysis,In order to know the specific meaning of the identifier.

such as:

void (* signal (int, void (* fp) (int))) (int);

What is signal?

Helix rule

For how to identify variables,There is an unofficial "clockwise/spiral rule" that can be used to help identify.

The content of the law,simply put,To understand the meaning of an unknown identifier,We can:

1. Start with the identifier we need to determine,Circle clockwise,When encountering the following symbols,Replace with the corresponding semantics:

[x] or [] =>array or array of capacity x (type1, type2 ...) =>function that receives type1, type2 ... and returns the value (to be determined) * =>Pointer to (type to be determined)

2. Repeat the above steps until all symbols in the statement have been traversed.

3. Always prioritize the parts enclosed in parentheses.

Field exercise

A simple example

Let's start with a simple one,Determine the meaning of str in the following statement:

+ ------- +

| +-+ |

| ^ | |

char * str [10];

^ ^ | |

| + --- + |

+ ----------- +

According to the law of spiral,As shown in the line diagram above,

Start with str, the object that needs to be determined. The first encounter on the spiral path is [left square bracket,From this we know thatstr is an array of size 10. Keep spinning,Encountered *, so str is an array of size 10, and the array elements are pointers. Continue and encounter;to mark the end of the statement. Moving on, we encounter char, so str is an array of size 10, and the elements of the array are pointers to type char.

Advanced

Back to the statement at the beginning of the article,To determine the meaning of signal.

+ ----------------------------- +

| + --- + |

| + --- + | +-+ | | |

| ^ | | ^ || | |

void (* signal (int, void (* fp) (int))) (int);

^ ^ | ^ ^ || |

| + ------ + | +-+ | |

| + -------- + |

+ ---------------------------------- +

Draw the above line graph by the spiral rule,Further analysis:

First encountered from the signal to be determined (The left parenthesis indicates that signal is a function,The input parameters are int and ...

Here we need to further use the spiral rule to determine the meaning of fp before we can confirm the complete input parameters of the signal function.So from fp, a sub-spiral is performed.

Because you need to parse the bracketed parts first,So the first time I come back is *, and thus fp is a pointer.

Continue parsing fp and encounter (, so fp is a pointer to a function,This function takes an input of type int.

continue,Encounter void, so fp is a pointer to a function,This function takes an input of type int and returns a null value.

At this point, the analysis of fp is completed, and we can know that the type of signal is:

Is a function,Entry parameters are: An int A pointer to a function,This function takes an input of type int and the return value is empty

The path runs into the spiral of the signal,Encountered * (immediately to the left of signal), so signal is

A function,Entry parameters are: An int A pointer to a function,This function takes an input of type int and the return value is empty The return value is a pointer

Continue, encountered (, continued from above, the return value is a pointer to another function,The pointed function accepts an int argument.

Finally, when void is encountered, the return value of the function pointed to by the signal return value is null.

Finally, the complete type of signal is:receive an int, a pointer to a function that receives an int and returns a null value,A function of these two parameters,And the return value points to a function that takes an int and returns null...orz.

Determination of member functions

Spiral casts do not give a decision with const participation,But because const decorates the element to its left by default,If there are no elements on the right,Modifies the element on the left.So just look at const and the elements it modifies,You can still use the spiral rule.

Consider the following statement:

const int * const method3 (const int * const&) const;

When a function is immediately followed by a const, it indicates that the member function's scope * this is a constant, that is, the entity of the class cannot be modified in the function body.

Let's analyze the above statement:

Starting from method3, we encounter (, so method3 is a function,Receives a reference as an input to the const int * const&part. The type of this reference is const int * const, a constant pointer to an integer constant. We continue to encounter * const, so the return value of the function is a constant pointer.The pointer points to a const int integer constant. The const at the end of the function is as described earlier,Identifies that the instance of the function body is not modified.

related resources

the `` clockwise/spiral rule "" the c++ "const" declaration:why&how

to sum up

c
  • Previous Java file archive and restore
  • Next Python performance measurement tool cProfile usage analysis