gpt4 book ai didi

c++ - 指向结构中作为静态成员的函数的指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:54 24 4
gpt4 key购买 nike

struct X {
int f(int);
static int f(long);
};

int (X::*p1)(int) = &X::f; // OK
int (*p2)(int) = &X::f; // error: mismatch
int (*p3)(long) = &X::f; // OK
int (X::*p4)(long) = &X::f; // error: mismatch
int (X::*p5)(int) = &(X::f); // error: wrong syntax for pointer to member
int (*p6)(long) = &(X::f); // OK

我认为 p1 和 p5 是同一种情况。为什么p5错了?

最佳答案

因为标准是这么说的。来自 N3936:

5.3.1 Unary operators

  1. A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [ Note: that is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type “pointer to member.” Neither does qualified-id, because there is no implicit conversion from a qualified-id for a non-static member function to the type “pointer to member function” as there is from an lvalue of function type to the type “pointer to function” (4.3). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id’s class. — end note ]

关于c++ - 指向结构中作为静态成员的函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com