随笔-8  评论-5  文章-0  trackbacks-0
    对识别C/C++中复杂的声明,Thinking in C++中介绍了一种简单的方法,right-left-right,即以变量为中心,向右走,一直到碰到右括号或不能再往右走,再往左走步,碰到左括号,再向右走。。。
    如对于void * fun(void),可以翻译为fun is a function that requires no argument and returns pointer to void。而void (* fun)(int),则可以理解为fun is a pointer to function that requires int argument and returns void。
    值得注意的是,对于像function这样的情况,必须先说argument,再说return value,两者都不可缺。例如,
void * (*set_handler(void (*f)))();
理解为,set_handler is a function that requires ... argument and returns pointer to function that requires no argument and returns pointer to void。
碰到具体的符号的读法如下:
id     'id' is a 
*      pointer to
(
---)   funciont that requires---argument(s) and returns
[
---]   array of 
type    type

    Thinking in C++中也提到,这种方法对大部分声明有效,里面没有提到无效的声明(我也想不到),可能是其他一些比较变态的声明。而The C programming language则从C的语法角度解释了复杂表达式的解读方法。
    dcl:           optional *'s direct-dcl
    direct-dcl:  name
                   (dcl)
                   direct
-dcl()
                   direct
-dcl[optional size]
    用top down的方法,即可很容易的出上面的结论了。具体请看相关章节The C Programming Language 5.12
posted on 2005-08-03 00:07 pumpkin 阅读(244) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。