C++变量类型——顺时针螺旋移动法
2025/9/1大约 2 分钟
C++有个令人头痛的点是会出现一些极其复杂的变量声明,比如:
string(const** f(int, void (*const p)(int)))(char[]);这会让人难以判断这个变量是什么类型。最近发现一个顺时针螺旋移动法则,感觉对于C++变量类型的确定很有用。
顺时针螺旋移动法
顺时针螺旋移动法的原文介绍和举例
一、主要步骤
- 重要的语法解释:
[X] or []: Array X size of... or Array undefined size of...
(type1, type2): function passing type1 and type2 returning...
*: pointer(s) to...
确定变量名,将其作为顺时针螺旋的起点
按照顺时针螺旋路径遍历所有修饰符
遇到括号,先遍历完括号内中的内容
二、举例解释
1.char *str[10]
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+- str is an...
- str is an array 10 of...
- str is an array 10 of pointers to...
- str is an array 10 of pointers to char
2.char *(*fp)( int, float *)
+--------------------+
| +---+ |
| |+-+| |
| |^ || |
char *(*fp)( int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+- fp is a...
- fp is a pointer to...
- fp is a pointer to a function passing an int and a pointer to float returning...
- fp is a pointer to a function passing an int and a pointer to float returning a pointer to...
- fp is a pointer to a function passing an int and a pointer to float returning a pointer to a char
3.void (*signal(int, void (*fp)(int)))(int)
+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+- signal is a function passing an int and a...
- fp is a pointer to...
- fp is a pointer to a function passing int returning...
- fp is a pointer to a function passing int returning nothing (void)
- signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning...
- signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to...
- signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning...
- signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning nothing (void)
