成员函数在什么方面可以相互比较? [英] In what ways member functions may be compared to each other?

查看:159
本文介绍了成员函数在什么方面可以相互比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我可以比较2个成员函数与<运算符。我可以做==但我不能使用它在下面的情况。我尝试将它们转换为 void * ,但不起作用。

I would like to know if I can compare 2 member functions with the "<" operator. I can do "==" but I can't use it in the case below. I tried casting them to void* but that won't work either.

template <class Receiver, class Sender>
class CallBack2 : public ICallBack2 {

protected:

    Receiver* receiver;
    void(Receiver::*function)(Sender*);
    Sender* sender;

public:

    CallBack2(Receiver* _receiver, void(Receiver::*_function)(Sender*), Sender* _sender) : receiver(_receiver), function(_function), sender(_sender) {};
    virtual ~CallBack2() {};

    virtual void callBack() {
        (receiver->*function)(sender);
    }

    virtual bool operator<(const ICallBack2* _other) const {
        CallBack2<Receiver, Sender>* other = (CallBack2<Receiver, Sender>*)_other;
        if (receiver < other->receiver) {
            return true;
        } else if (receiver == other->receiver && function < other->function) {
            return true; // this line gives the error
        }
        return false;
    }
};

有任何想法吗?

推荐答案

如果你只是想任意地将它们设置为set / map中的键,那么你可以 reinterpret_cast 。您可能需要一个类似 exact_int< sizeof(void(Foo :: * bar)())> :: type 的模板类,因为指向成员函数的指针可以有有趣的大小

If you just want to arbitrarily order them to be keys in a set/map, then you can reinterpret_cast them. You may need a template class like exact_int<sizeof(void (Foo::*bar)())>::type because pointers to member functions can have funny sizes.

这篇关于成员函数在什么方面可以相互比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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