且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将“成员指针指向派生类"转换为“指向基类的成员指针";

更新时间:2023-02-14 23:02:24

简单的答案是,C ++没有定义您尝试的转换,因此程序格式错误.

The simple answer, is that C++ does not define the conversion you are attempting and thus your program is ill formed.

考虑标准转换(C ++11§4/1):

Consider standard conversions (C++11§4/1):

标准转换是具有内置含义的隐式转换.第4章列举了此类转换的全部内容.

Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions.

由于您没有执行任何强制转换,也没有定义任何自定义转换,因此实际上您正在执行这种标准转换.在没有列举所有可能的此类转换的情况下,您的示例明确涉及了两个: pointer 转换和 pointer to member 转换.请注意,C ++并不将指向成员类型的指针视为指针类型的子集.

Since you are not performing any cast, nor do you have any custom conversions defined, you are indeed performing such a standard conversion. Without enumerating all possible such conversions, two are of explicit interest for your example: pointer conversions and pointer to member conversions. Note that C++ does not consider pointer to member types to be a subset of pointer types.

指向成员转换的指​​针在C ++11§4.11中定义,并且正好包含两个转换:

Pointer to member conversions are defined in C++11§4.11 and consist of exactly two conversions:

  • 空成员指针转换,它允许将空指针常量转换为指向成员类型(4.11/1)的指针.
  • 人为设计的第二转换(4.11/2):

    • The null member pointer conversion which allows null pointer constants to be converted to pointer to member types (4.11/1).
    • The somewhat more contrived second conversion (4.11/2):

      指向cv T类型的B的成员的指针",其中B是类类型,可以转换为指向cv T类型的D的成员的指针",其中D是派生类[...] of B

      • 空指针转换(4.10/1),它允许将空指针常量转换为指针类型.
      • 转换为指向 void 的指针(4.10/2),这允许将任何指针类型转换为指向 void 的指针.
      • 最后是为什么它可以使用指针(而不是指向成员的指针)(4.1​​0/3):

        • The null pointer conversion (4.10/1) which allows null pointer constants to be converted to pointer types.
        • Conversion to pointers to void (4.10/2) which allows the conversion of any pointer type to pointer to void.
        • And finally why it works with pointers (but not pointers to member) (4.10/3):

          其中D是类类型的[[cv​​ D的指针],可以转换为[cv B的指针],其中B是基类[...]D的]