<i id='jipcU'><tr id='jipcU'><dt id='jipcU'><q id='jipcU'><span id='jipcU'><b id='jipcU'><form id='jipcU'><ins id='jipcU'></ins><ul id='jipcU'></ul><sub id='jipcU'></sub></form><legend id='jipcU'></legend><bdo id='jipcU'><pre id='jipcU'><center id='jipcU'></center></pre></bdo></b><th id='jipcU'></th></span></q></dt></tr></i><div id='jipcU'><tfoot id='jipcU'></tfoot><dl id='jipcU'><fieldset id='jipcU'></fieldset></dl></div>
    • <bdo id='jipcU'></bdo><ul id='jipcU'></ul>

      <small id='jipcU'></small><noframes id='jipcU'>

    1. <tfoot id='jipcU'></tfoot>

      1. <legend id='jipcU'><style id='jipcU'><dir id='jipcU'><q id='jipcU'></q></dir></style></legend>

        为什么使用函数名作为函数指针等同于将地址运算符应用于函数名?

        Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?(为什么使用函数名作为函数指针等同于将地址运算符应用于函数名?)
          <bdo id='ZLZBK'></bdo><ul id='ZLZBK'></ul>

          <tfoot id='ZLZBK'></tfoot>
          <i id='ZLZBK'><tr id='ZLZBK'><dt id='ZLZBK'><q id='ZLZBK'><span id='ZLZBK'><b id='ZLZBK'><form id='ZLZBK'><ins id='ZLZBK'></ins><ul id='ZLZBK'></ul><sub id='ZLZBK'></sub></form><legend id='ZLZBK'></legend><bdo id='ZLZBK'><pre id='ZLZBK'><center id='ZLZBK'></center></pre></bdo></b><th id='ZLZBK'></th></span></q></dt></tr></i><div id='ZLZBK'><tfoot id='ZLZBK'></tfoot><dl id='ZLZBK'><fieldset id='ZLZBK'></fieldset></dl></div>
          <legend id='ZLZBK'><style id='ZLZBK'><dir id='ZLZBK'><q id='ZLZBK'></q></dir></style></legend>

          <small id='ZLZBK'></small><noframes id='ZLZBK'>

                <tbody id='ZLZBK'></tbody>

                1. 本文介绍了为什么使用函数名作为函数指针等同于将地址运算符应用于函数名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有趣的是,使用函数名作为函数指针相当于将地址运算符应用于函数名

                  这是示例.

                  typedef bool (*FunType)(int);
                  bool f(int);
                  int main() {
                    FunType a = f;
                    FunType b = &a; // Sure, here's an error.
                    FunType c = &f; // This is not an error, though. 
                                    // It's equivalent to the statement without "&".
                                    // So we have c equals a.
                    return 0;
                  }
                  

                  使用名称是我们在数组中已经知道的东西.但是你不能写像

                  Using the name is something we already know in array. But you can't write something like

                  int a[2];
                  int * b = &a; // Error!
                  

                  它似乎与语言的其他部分不一致.这种设计的原理是什么?

                  It seems not consistent with other parts of the language. What's the rationale of this design?

                  这个问题解释了这种行为的语义以及它为什么起作用.但我对为什么这样设计语言很感兴趣.

                  This question explains the semantics of such behavior and why it works. But I'm interested in why the language was designed this way.

                  更有趣的是,函数类型作为参数使用时可以隐式转换为指向自身的指针,但作为返回类型使用时不会转换为指向自身的指针!

                  What's more interesting is the function type can be implicitly converted to pointer to itself when using as a parameter, but will not be converted to a pointer to itself when using as a return type!

                  示例:

                  typedef bool FunctionType(int);
                  void g(FunctionType); // Implicitly converted to void g(FunctionType *).
                  FunctionType h(); // Error!
                  FunctionType * j(); // Return a function pointer to a function 
                                      // that has the type of bool(int).
                  

                  推荐答案

                  由于您特别询问这种行为的基本原理,这是我能找到的最接近的内容(来自 ANSI C90 基本原理文档 - http://www.lysator.liu.se/c/rat/c3.html#3-3-2-2):

                  Since you specifically ask for the rationale of this behavior, here's the closest thing I can find (from the ANSI C90 Rationale document - http://www.lysator.liu.se/c/rat/c3.html#3-3-2-2):

                  3.3.2.2 函数调用

                  3.3.2.2 Function calls

                  函数指针可以用作(*pf)()pf().后一种结构,未在基础文件中批准,出现在一些当前版本的 C,是明确的,不会使旧代码失效,并且可以是一个重要的速记.速记是有用的只提供一个外部名称的包,它指定一个充满指向对象和函数的指针的结构:成员函数可以被称为 graphics.open(file) 而不是(*graphics.open)(file).函数指示符的处理可以导致一些奇怪但有效的句法形式.鉴于声明:

                  Pointers to functions may be used either as (*pf)() or as pf(). The latter construct, not sanctioned in the Base Document, appears in some present versions of C, is unambiguous, invalidates no old code, and can be an important shorthand. The shorthand is useful for packages that present only one external name, which designates a structure full of pointers to object s and functions : member functions can be called as graphics.open(file) instead of (*graphics.open)(file). The treatment of function designators can lead to some curious , but valid , syntactic forms . Given the declarations :

                  int f ( ) , ( *pf ) ( ) ; 
                  

                  那么以下所有表达式都是有效的函数调用:

                  then all of the following expressions are valid function calls :

                  ( &f)(); f(); (*f)(); (**f)(); (***f)();
                  pf(); (*pf)(); (**pf)(); (***pf)();
                  

                  每一行的第一个表达式在前面已经讨论过段落 .二是常规用法.所有后续表达式利用函数的隐式转换指针值的指示符,几乎在所有表达式上下文中.委员会认为允许这些形式没有任何实际危害;取缔像 (*f)() 这样的形式,同时仍然允许 *a (for int a[]),只是看起来比它的价值更麻烦.

                  The first expression on each line was discussed in the previous paragraph . The second is conventional usage . All subsequent expressions take advantage of the implicit conversion of a function designator to a pointer value , in nearly all expression contexts . The Committee saw no real harm in allowing these forms ; outlawing forms like (*f)(), while still permitting *a (for int a[]), simply seemed more trouble than it was worth .

                  基本上,增加了函数指示符和函数指针之间的等价性,使使用函数指针更加方便.

                  Basically, the equivalence between function designators and function pointers was added to make using function pointers a little more convenient.

                  这篇关于为什么使用函数名作为函数指针等同于将地址运算符应用于函数名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  <i id='rWpOd'><tr id='rWpOd'><dt id='rWpOd'><q id='rWpOd'><span id='rWpOd'><b id='rWpOd'><form id='rWpOd'><ins id='rWpOd'></ins><ul id='rWpOd'></ul><sub id='rWpOd'></sub></form><legend id='rWpOd'></legend><bdo id='rWpOd'><pre id='rWpOd'><center id='rWpOd'></center></pre></bdo></b><th id='rWpOd'></th></span></q></dt></tr></i><div id='rWpOd'><tfoot id='rWpOd'></tfoot><dl id='rWpOd'><fieldset id='rWpOd'></fieldset></dl></div>
                  <tfoot id='rWpOd'></tfoot>
                  • <legend id='rWpOd'><style id='rWpOd'><dir id='rWpOd'><q id='rWpOd'></q></dir></style></legend>
                      1. <small id='rWpOd'></small><noframes id='rWpOd'>

                          <bdo id='rWpOd'></bdo><ul id='rWpOd'></ul>
                            <tbody id='rWpOd'></tbody>