In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The content of this article mainly explains "what is the spiral rule of C++ variable determination", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C++ variable determination of the spiral rule is what" it!
Preface
An identifier in C++ with a variety of modified delimiters, so that the original meaning of the identifier can not be seen at a glance, and even need to be carefully analyzed to know the specific meaning of the identifier.
For example:
Void (* signal (int, void (* fp) (int) (int)
What is signal among them?
Helix rule
There is an unofficial "clockwise / helix rule (Clockwise/Spiral Rule)" that can be used to help identify variables.
The content of this rule, simply put, in order to understand the meaning of an unknown identifier, we can:
1. Start with the identifier we need to determine, circle clockwise, and replace it with the corresponding semantics when you encounter the following symbols:
[X] or [] = > Array or array with capacity x (type1,type2...) = > receive type1, type2... And returns a pointer to (type to be determined)
Repeat the above steps until all the symbols in the statement have been traversed.
Always give priority to parsing the parts enclosed in parentheses.
Field exercise
A simple example
Starting with a simple one, determine the meaning of str in the following statement:
+-+ | +-+ | | ^ | char * str [10]; ^ | +-- + | +-+
According to the helix rule, as shown in the above chart logo
Starting from str, the object that needs to be determined. The first encounter on the spiral path is the [left square bracket, so we know that str is an array of size 10. Continue to rotate and encounter *, so str is an array of size 10, with array elements as pointers. Continue, encounter; identify the end of the statement. Go on, encounter char, so str is an array of size 10, and the array elements are pointers to the char type.
Advanced level
Go back to the sentence at the beginning of the article to determine the meaning of signal.
+-- + | +-- + | | +-+ | +-+ | ^ | | ^ | | void (* signal (int, void (* fp) (int) (int) ^ ^ | ^ ^ | | +-+
The diagram as above is drawn by the helix rule, which can be analyzed:
First encounter from the signal to be determined (left parenthesis, indicating that signal is a function, input parameters are int and.
Here, we need to further use the helix rule to determine the meaning of fp before we can confirm the complete input parameter of the function signal. So the hair from fp carries on a sub-helix.
Because you need to parse the parts enclosed in parentheses first, the first thing you encounter when you turn around is *, so fp is a pointer.
Continue parsing fp and encounter (, so fp is a pointer to a function that takes an input parameter of type int.
Go ahead and encounter void, so fp is a pointer to a function that takes an input parameter of type int and returns a null value.
Now that the parsing of fp is completed, you can know that the type of signal is:
Is a function with an input parameter of type int and a pointer to a function, which takes an input parameter of type int and returns a null value
The path runs into the spiral of signal and encounters * (next to the left side of signal), so signal is
A function with an input parameter of type int and a pointer to a function that takes an input parameter of type int and returns a null value as a pointer
Continue, encounter (, then above, the return value is a pointer to another function, and the function being pointed to receives an int input parameter.
Finally, the return value of the function that the void,signal return value points to is empty.
Finally, the full type of signal is to receive an int, a pointer to a function that receives an int and returns a null value, a function with these two parameters, and a return value that points to a function that receives a null return of type int. Orz.
Determination of member function
Helix casting does not give a decision in the case of const participation, but because const modifies the element on its left by default, if there is no element on the right, it modifies the element on the left. So as long as you look at const and the elements it modifies as a whole, you can still use the helix rule.
Examine the following sentences:
Const int*const Method3 (const int*const&) const
When a const is followed by a function, it indicates that * this in the scope of the member function is constant, that is, the entity of the class cannot be modified in the body of the function.
The above statement is analyzed below:
Starting from Method3, encounter (, so Method3 is a function that receives a reference as the input parameter const int*const&. The reference is of type const int*const, a constant pointer to the shaping constant. Continue to encounter * const, so the return value of the function is a constant pointer. The pointer points to a const int shaping constant of the type. The const at the end of the function, as mentioned earlier, identifies the body of the function without modifying the instance.
At this point, I believe that everyone on the "C++ variable determination of the spiral rule is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.