In what ways member functions may be compared to each other?(成员函数可以通过哪些方式相互比较?)
问题描述
我想知道是否可以将 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.
这篇关于成员函数可以通过哪些方式相互比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:成员函数可以通过哪些方式相互比较?
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
