从指向基 class 成员函数的指针派生 class 成员函数的指针

Pointer to derivate class member function from pointer to base class member function

编译此程序的 C++ 规则是什么?

struct A
{ void f() {}; };

struct B : A
{};

int main()
{
    void (B::*ptr)() = &A::f;
    (void)ptr;
    return 0;
}

我知道A::f是classB的成员,但是允许这个赋值的具体规则是什么?我对此有点困惑。编译器是否检查 A::f 的地址是 B 的成员还是什么?

使 ptr 的初始化格式良好的标准的诗句是

[conv.mem]/2 A prvalue of type “pointer to member of B of type cv T”, where B is a class type, can be converted to a prvalue of type “pointer to member of D of type cv T”, where D is a complete class derived ([class.derived]) from B...

希望对您有所帮助。