Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带有指向成员函数指针的模板_C++_Templates_Pointers - Fatal编程技术网

C++ 带有指向成员函数指针的模板

C++ 带有指向成员函数指针的模板,c++,templates,pointers,C++,Templates,Pointers,我试图为函数对象编写一个模板,作为STL算法函数中的谓词。我对这个还很陌生,我不能完全理解 以下是模板: #ifndef MEMBERPREDICATE_H #define MEMBERPREDICATE_H template <typename Type, typename Class> class MemberPredicate{ public: MemberPredicate(Type (Class::*method)(), bool (*operation)(Typ

我试图为函数对象编写一个模板,作为STL算法函数中的谓词。我对这个还很陌生,我不能完全理解

以下是模板:

#ifndef MEMBERPREDICATE_H
#define MEMBERPREDICATE_H

template <typename Type, typename Class>
class MemberPredicate{
public:
    MemberPredicate(Type (Class::*method)(), bool (*operation)(Type,Type), Type data) : method(method), operation(operation), data(data){};
    bool operator()(Class * other){ return *(this->operation)(other->*(this->method)(), this->data);}
private:
    Type (Class::*method)();
    bool (*operation)(Type,Type);
    Type data;
};
#endif
这里的标记位于箭头
other->
我尝试将箭头链接到点上,并随意移动它们,但无法使其工作

正如我所说,我对这一点还不熟悉,很可能这是一种非常奇怪的方式。如果是这样的话,正确的方向是非常受欢迎的,但我仍然希望掌握这一部分

最后,我有一个额外的问题,我想在我完成这部分工作后使get方法常量,但我不确定如何,这里的任何帮助也会很好

谢谢

*(此->操作)(其他->*(此->方法)(),此->数据);
^                         ^
由于运算符优先级,调用运算符
()
在函数指针取消引用之前被调用。只有在调用call运算符后,才会取消对它的引用

将其更改为:

(*此->操作)((其他->*(此->方法))(),此->数据);
^                         ^
注意,我还在
other->*(this->method)()
周围放了另一对括号。这是必需的,因为
(此->方法)(
将在
其他->*
之前进行评估


此外,正如@dyp所说,您省略了大部分
this
限定符,从而得出以下结果:

操作((其他->*方法)(),数据);

如果可以,请使用
std::bind
other->*(this->method)(
应该是
(other->*this->method)(
IIRC。已经考虑过涉及/?对于成员函数类型,至少一个简单的
typedef
会对IMHO有所帮助。正如我所说,我是新手,我甚至没有听说过std::bind或std::function,但它们看起来很有趣
(*此->操作)
也可以是
此->操作。指向函数的指针不需要间接寻址。不确定什么更可读(反正很难看)。哦,
这个
可以省略,当然:
操作((其他->*方法)(),数据)@dyp Update:好的,我会更改它。@dyp语法真的让我着迷了。:)好的,现在是正确的:)我希望这很好地表明语法应该不惜一切代价避免..非常感谢,我在运算符优先级方面做得更好了,这让我很困惑。另外,谢谢dyp,这真的让整个事情更容易理解
bool equal(int a, int b){
    return a == b;
}

MemberPredicate<int, IntClass> pred(&IntClass::getInt, &equal, 4)
IntClass * i = new IntClass(5) // stores 5 in IntClass and returns this value when i->getInt() is called
pred(i);
Must use '.*' or '->*' to call pointer to member function in Member...
bool operator()(Class * other){ return *(this->operation)(other->*(this->method)(), this->data);}