C ++语法/语义问题:参考Function和typedef关键字 [英] C++ Syntax/Semantics Question: Reference to Function and typedef keyword

查看:256
本文介绍了C ++语法/语义问题:参考Function和typedef关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef int(& rifii)(int,int)会用于什么?



是这个语句之前的typedef吗?
我想这个是

  typedef(int(& rifii)新名称] 

但是[新名称]不存在,如果您

  typedef int INTEGER; 

以下语法的类似问题:

  typedef void(* PF)(); 

PF edit_ops [] = {& cut,& paste,& copy,& search};
PF file_ops [] = {& open,& append,&关闭,&写};

PF * button2 = edit_ops;
PF * button3 = file_ops;

button2 [2]();

typedef允许什么?它是使它,所以你不必键入:

  void(* PF)(); 
(void(* PF)())edit_ops [] = {& cut,& paste,& copy,& search};
(void(* PF)())file_ops [] = {& open,& append,& close,& write};

(void(* PF)())* button2 = edit_ops;
(void(* PF)())* button3 = file_ops;

如果是这样,发生到typedef的第二部分(你想要什么)

  typedef [你有什么 - (FP)] [你想要什么] 

解决方案

p> Typedef不起作用 typedef [type] [new name] [new name] 部分并不总是在结尾。



如果 [一些声明] 声明一个变量, typedef [same declaration] 将定义一个类型。



例如:




  • int x; 声明一个名为x的变量int - > typedef int x;
    定义一个x类型为int。

  • code> struct {char c; }; 定义一个名为s的变量类型 - > typedef struct {char c;
  • c> c>

    命名为p的类型指针int - >
    typedef int * p; 定义一个类型p作为指向int的指针


      并且:




      • int A []; / code>声明一个名为A - > typedef int A的数组数组[]; 将类型A声明为int数组。

      • int f(); 声明一个名为f - > 的函数typedef int f();

      • int g(int); 声明一个函数名g - > typedef int g(int); 声明一个函数类型g作为返回int并获取一个int。



      另外:请注意,所有的函数参数都在新的名字之后!由于这些类型也可能很复杂,在[新名称]后面可能会有很多文本。可悲的是,但是真的。



      但是那些不是正确的函数指针,只是函数类型。我不确定一个函数类型存在于C或C ++,但它作为一个中间步骤在我的解释。



      要创建一个真正的函数指针,我们有以向名称添加*。

      声明一个函数类型pf作为返回a int * 。噢,这不是想要的。


    因此,使用()分组:




    • typedef int(* pf)(); 声明一个函数指针类型pf作为返回int并且不带参数。 >
    • typedef int(& rf)(); 声明一个函数引用类型rf作为返回int并且不带参数。


    现在让我们来看看你的例子并回答你的问题:



    typedef int(& rifii)(int,int); 声明一个函数引用类型rifii,返回一个int并带两个int参数。



    显然(?) button2 [2](); 会调用 copy(); 。 >

    没有typedef的正确语法在没有编译器的情况下很难正确编写,即使使用编译器也很难读:

      void(* edit_ops [])()= {& cut,& paste,& copy,& search}; 
    void(* file_ops [])()= {& open,& append,&关闭,&写};

    void(** button2)()= edit_ops;
    void(** button3)()= file_ops;

    button2 [2]();

    这是为什么当使用函数指针时,大家更喜欢typedef。



    阅读时,找到开始阅读的地方。阅读尽可能多的权利,你可以,但观察分组由()。然后尽可能地向左边读,再次限于grouping()。



    应用于 void(* edit_ops []) (),表示


    1. edit_ops是
      >
    2. 数组
      (点击组的末尾,所以向左转)

    3. 指针


    4. 的函数(向右解析())

    5. 无参数

    6. 返回空白

    对于专家:
    为了使它更复杂,参数可以有名称(将被忽略),所以它甚至可能很难找到在哪里开始解析!例如。 typedef int(* fp)(int x); 是有效的,并且与 typedef int(* fp)(int); 名称甚至可以有()在它们周围: typedef int(* fp)(int(x)); 但是正如我们所看到的, ,所以即使允许以下: typedef int(* fp)(int()); 。这仍然是一个函数指针,接受一个int并返回一个int。如果你想让你的代码真的很难阅读...


    What would typedef int (&rifii) (int, int) be used for?

    What is the typedef before this "statement" do? I want to think of this as

    typedef (int (&rifii) (int, int)) [new name]
    

    but the [new name] is not there like if you do

    typedef int INTEGER;
    

    Similar question for the following syntax:

    typedef void (*PF) ();
    
    PF edit_ops[ ] = { &cut, &paste, &copy, &search };
    PF file_ops[ ] = { &open, &append, & close, &write };
    
    PF* button2 = edit_ops;
    PF* button3 = file_ops;
    
    button2[2]( );
    

    What is the typedef allowing? Is it making it so you don't have to type:

    void (*PF) ();
    (void (*PF) ()) edit_ops[ ] = { &cut, &paste, &copy, &search };
    (void (*PF) ()) file_ops[ ] = { &open, &append, & close, &write };
    
    (void (*PF) ())* button2 = edit_ops;
    (void (*PF) ())* button3 = file_ops;
    

    If so what happend to the second part ([what you want]) of the typedef like in:

    typedef [what you have -- (FP)] [what you want]
    

    Clarification on this matter is greatly appreciated.

    解决方案

    Typedef does not work like typedef [type] [new name]. The [new name] part does not always come at the end.

    You should look at it this way: if [some declaration] declares a variable, typedef [same declaration] would define a type.

    E.g.:

    • int x; declares a variable named x of type int -> typedef int x; defines a type x as int.
    • struct { char c; } s; defines a variable named s of some struct type -> typedef struct { char c; } s; defines type s to be some struct type.
    • int *p; declares a variable named p of type pointer to int -> typedef int *p; defines a type p as pointer to int.

    And also:

    • int A[]; declares an array of ints named A -> typedef int A[]; declares a type A as an array of ints.
    • int f(); declares a function named f -> typedef int f(); declares a function type f as returning an int and taking no arguments.
    • int g(int); declares a function name g -> typedef int g(int); declares a function type g as returning an int and taking one int.

    As an aside: Note that all function arguments come after the new name! As those types could be complicated as well, there can be a lot of text after the [new name]. Sadly, but true.

    But those are not proper function pointers yet, just function types. I'm not sure a function type exists in C or C++, but it is useful as an intermediate step in my explanation.

    To create a real function pointer, we have to add '*' to the name. Which, sadly, has wrong precedence:

    • typedef int *pf(); declares a function type pf as return a int*. Oops, that's not what was intended.

    So use () to group:

    • typedef int (*pf)(); declares a function pointer type pf as returning an int and taking no arguments.
    • typedef int (&rf)(); declares a function reference type rf as returning an int and taking no arguments.

    Let's now look at your examples and answer your questions:

    typedef int (&rifii) (int, int); declares a function reference type rifii as returning an int and taking two int arguments.

    And obviously (?) button2[2]( ); will call copy();.

    Proper syntax without typedefs is hard to write correctly without a compiler, and hard to read even with a compiler:

    void (*edit_ops[])() = { &cut, &paste, &copy, &search }; 
    void (*file_ops[])() = { &open, &append, & close, &write };
    
    void (**button2)() = edit_ops;
    void (**button3)() = file_ops;
    
    button2[2]( );   
    

    Which is why everyone prefers typedefs when using function pointers.

    When reading, find the place to start reading. Read as much to the right as you can, but observe the grouping by (). Then read to the left as much as you can, again limited by grouping (). After finishing everything inside the (), start with reading to the right, then to the left.

    Applied to void (*edit_ops[])(), this means that

    1. edit_ops is (go to the right)
    2. an array (hit the end of the group, so turn to the left)
    3. of pointer (end of grouping)
    4. to a function taking (parse the () to the right)
    5. no arguments (go to the left)
    6. returning a void

    For the experts: To make it even more complicated, arguments can have names (which will be ignored), so it might even be hard to find where to start parsing! E.g. typedef int (*fp)(int x); is valid and the same as typedef int (*fp)(int); Names can even have () around them: typedef int (*fp)(int (x)); But as we have seen, argument names can be left out, so even the following is allowed: typedef int (*fp)(int ());. This is still a function pointer taking a single int and return an int. In case you would like to make your code really hard to read...

    这篇关于C ++语法/语义问题:参考Function和typedef关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆