为什么函数指针定义可以使用任意数量的 & 符号“&"或星号'*'? [英] Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?

查看:32
本文介绍了为什么函数指针定义可以使用任意数量的 & 符号“&"或星号'*'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要做以下工作?

void foo() {
    cout << "Foo to you too!
";
};

int main() {
    void (*p1_foo)() = foo;
    void (*p2_foo)() = *foo;
    void (*p3_foo)() = &foo;
    void (*p4_foo)() = *&foo;
    void (*p5_foo)() = &*foo;
    void (*p6_foo)() = **foo;
    void (*p7_foo)() = **********************foo;

    (*p1_foo)();
    (*p2_foo)();
    (*p3_foo)();
    (*p4_foo)();
    (*p5_foo)();
    (*p6_foo)();
    (*p7_foo)();
}

推荐答案

有几个部分可以让所有这些运算符组合以相同的方式工作.

There are a few pieces to this that allow all of these combinations of operators to work the same way.

所有这些工作的根本原因是一个函数(如 foo)可以隐式转换为指向该函数的指针.这就是 void (*p1_foo)() = foo; 起作用的原因:foo 被隐式转换为指向自身的指针,并将该指针分配给 p1_foo.

The fundamental reason why all of these work is that a function (like foo) is implicitly convertible to a pointer to the function. This is why void (*p1_foo)() = foo; works: foo is implicitly converted into a pointer to itself and that pointer is assigned to p1_foo.

一元 & 应用于函数时,会产生指向该函数的指针,就像应用于对象时会产生对象的地址一样.对于指向普通函数的指针,由于隐式的函数到函数指针的转换,它总是多余的.无论如何,这就是 void (*p3_foo)() = &foo; 起作用的原因.

The unary &, when applied to a function, yields a pointer to the function, just like it yields the address of an object when it is applied to an object. For pointers to ordinary functions, it is always redundant because of the implicit function-to-function-pointer conversion. In any case, this is why void (*p3_foo)() = &foo; works.

一元*,当应用于函数指针时,产生被指向的函数,就像当它应用于指向一个对象的普通指针时产生被指向的对象一样.

The unary *, when applied to a function pointer, yields the pointed-to function, just like it yields the pointed-to object when it is applied to an ordinary pointer to an object.

这些规则可以组合.考虑倒数第二个例子,**foo:

These rules can be combined. Consider your second to last example, **foo:

  • 首先,foo 被隐式转换为指向自身的指针,第一个 * 应用于该函数指针,产生函数 foo
  • 然后,结果再次隐式转换为指向自身的指针,并应用第二个 *,再次生成函数 foo.
  • 然后它又被隐式转换为函数指针并赋值给变量.
  • First, foo is implicitly converted to a pointer to itself and the first * is applied to that function pointer, yielding the function foo again.
  • Then, the result is again implicitly converted to a pointer to itself and the second * is applied, again yielding the function foo.
  • It is then implicitly converted to a function pointer again and assigned to the variable.

你可以添加任意多的*,结果总是一样的.* 越多越好.

You can add as many *s as you like, the result is always the same. The more *s, the merrier.

我们也可以考虑你的第五个例子,&*foo:

We can also consider your fifth example, &*foo:

  • 首先,foo 被隐式转换为指向自身的指针;应用一元 *,再次产生 foo.
  • 然后,将 & 应用于 foo,产生一个指向 foo 的指针,该指针被分配给变量.
  • First, foo is implicitly converted to a pointer to itself; the unary * is applied, yielding foo again.
  • Then, the & is applied to foo, yielding a pointer to foo, which is assigned to the variable.

& 只能应用于函数,不能应用于已转换为函数指针的函数(当然,除非函数指针是变量,在这种情况下结果是指向函数的指针;例如,您可以将void (**pp_foo)() = &p7_foo;)添加到列表中.

The & can only be applied to a function though, not to a function that has been converted to a function pointer (unless, of course, the function pointer is a variable, in which case the result is a pointer-to-a-pointer-to-a-function; for example, you could add to your list void (**pp_foo)() = &p7_foo;).

这就是 &&foo 不起作用的原因:&foo 不是函数;它是一个函数指针,它是一个右值.但是,&*&*&*&*&*&*foo 会起作用,&******&foo 也会起作用>,因为在这两个表达式中,& 总是应用于函数而不是右值函数指针.

This is why &&foo doesn't work: &foo is not a function; it is a function pointer that is an rvalue. However, &*&*&*&*&*&*foo would work, as would &******&foo, because in both of those expressions the & is always applied to a function and not to an rvalue function pointer.

还要注意,您不需要使用一元 * 来通过函数指针进行调用;(*p1_foo)();(p1_foo)(); 都有相同的结果,同样是因为函数到函数指针的转换.

Note also that you do not need to use the unary * to make the call via the function pointer; both (*p1_foo)(); and (p1_foo)(); have the same result, again because of the function-to-function-pointer conversion.

这篇关于为什么函数指针定义可以使用任意数量的 & 符号“&amp;"或星号'*'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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