加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 虚拟成员函数的指针是否具有可比性?

发布时间:2020-12-16 07:11:24 所属栏目:百科 来源:网络整理
导读:我读了 an article说 A member function pointer can be set to 0,and provides the operators == and !=,but only for member function pointers of the same class. 我正在努力理解C 11,§5.10. 在§5.10/ 1中它说 Pointers of the same type (after point
我读了 an article说

A member function pointer can be set to 0,and provides the operators
== and !=,but only for member function pointers of the same class.

我正在努力理解C 11,§5.10.

在§5.10/ 1中它说

Pointers of the same type (after pointer conversions) can be compared
for equality.

在§5.10/ 2中它说

Otherwise if either is a pointer to a virtual member function,the
result is unspecified.

现在考虑以下测试程序.

#include <cassert>

class ISomeClass
{
public:
   virtual ~ISomeClass() {}

   virtual void a() = 0;
   virtual void b() = 0;
};

int main()
{
   typedef void(ISomeClass::*MemberPtr)();

   MemberPtr mp = &ISomeClass::a;

   assert( mp == &ISomeClass::a );
   assert( mp != &ISomeClass::b );

   return 0;
}

根据标准,断言是真的还是未指定的?

解决方法

我认为指向成员的一般指针是对象地址的转换.在指向虚方法的情况下,我认为细节取决于实现.在一般情况下,我认为这样做是个坏主意,但是如果指针在asignation期间被解析为正确的覆盖,它必须作为成员函数的标准指针,并且比较是移位比较和方法签名比较.检查reinterpret_cast以查看发生的情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读