C ++ variable determination screw rule

C ++, an identifier with a variety of modifications delimiters such intention identifier is less intuitive at a glance, even require careful analysis, you can know the specific meaning of the identifier.

such as:

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

What signal is the where?

Corkscrew rule

For how identification variables, there unofficial “clockwise / screw rule (Clockwise / Spiral Rule)” can be used to help identification.

The content of the law, in simple terms, in order to figure out the meaning of an unknown identifier, we can:

    We need to start from the determined identifier circle clockwise, encountered the following symbols and replaced with the corresponding semantics:

    [X] or [] => x capacity of the array or an array

    (Type1, type2 …) => receiving type1, type2 … function returns a value of (TBD)

    * => Points (determined type) pointer

    Repeat the above steps until all the symbols are the statement traversed.

    Always take precedence resolve bracketed section.

Field exercise

A simple example

Start with a simple start, determine the meaning of the following statement str:

                     | +-+   |
                     | ^ |   |
                char *str[10];
                 ^   ^   |   |
                 |   +---+   |

The screw rule, as shown in the above chart identification,

    From this str objects need to be determined.

    A first helical path is encountered [left bracket, whereby we know, str is an array size of 10.

    Continues to rotate, met *, it is a dimension str array 10, the array of pointer elements.

    Continue to encounter; to identify the end of the statement.

    Continue encountered char, so str is a dimension of the array 10, the array type element is to char pointer.


Back to the beginning of the article that statement to determine the meaning of the signal.

                      |                  +---+      |
                      |  +---+           |+-+|      |
                      |  ^   |           |^ ||      |
                void (*signal(int, void (*fp)(int)))(int);
                 ^    ^      |      ^    ^  ||      |
                 |    +------+      |    +--+|      |
                 |                  +--------+      |

As shown by the line in FIG screw rule, and further can be analyzed:

    To determine the signal from the first encounter (left parenthesis indicates signal is a function, and the parameters for the int …

    Here we need to need to further the use of screw rule to determine the meaning of fp, before it can confirm the complete signal of this function to the Senate. Therefore, once the sub-coil fp from the hair.

    Because the bracketed portion of the need to give priority to resolve, so turn around to come back for the first time encountered *, thus fp is a pointer.

    Continue to resolve fp, encountered (so fp is a pointer to a function, a receiving function of the parameters of type int.

    Continue encountered void, so fp is a pointer to a function, a receiving function of type int and returns a value of the parameter space.

  • 至此完成了 fp 的解析,可以知道 signal 的类型为:
    • 是一个函数,入参为:

        A type int

        A pointer to a function, a receiving function of type int and returns the reference value is null

  • 路径跑到 signal 的螺旋中,遇到 *(紧邻 signal 左边),所以 signal
    • 一个函数,入参为:

        A type int

        A pointer to a function, a receiving function of type int and returns the reference value is null

    • The return value is a pointer

  • Continue encountered (, then above, the return value is a pointer points to another function, this is directed into the int function receives a reference.

    Finally, experience void, signal the return value of the function pointed to the return value is null.

Finally, signal stroke at the full type: receiving an int, a pointer to an int and returns a value of the received null function pointer, the function of these two parameters, return value and a receiving point type int function returns null. ..Orz.

Determination member function

Spiral magic did not give judgment in the case of const participation, but because const default modified elements close to its left, the right, if no element, the element on the left is modified. Const and therefore only need to modify its elements as a whole, you can still use screw rule.

Visit the following statement:

const int*const Method3(const int*const&) const;

When the function is immediately behind a const, indicates the role of the member art * this function is a constant, i.e., the class can not be modified in the in vivo function entity.

The following analysis of the above statement:

    Starting from Method3 encountered (so Method3 is a function that takes a parameter incorporated into const int * const & section.

    This type of reference is const int * const, const pointer shaping constant.

    Continue to experience * const, so the function returns a value constant pointer. Type const int pointer to a constant integer.

    const end of the previously described functions, identification functions in vivo does not modify the examples.

related resources

  • The “Clockwise/Spiral Rule”
  • The C++ ‘const’ Declaration: Why & How

Leave a Reply