简体   繁体   English

带有成员函数指针的模板

[英]Template with pointer to member function

I'm trying to write a template for a function object to act as a predicate in a STL algorithm function. 我正在尝试为函数对象编写一个模板,以充当STL算法函数中的谓词。 Im still quite new to this and i cant get it quite right 我对此仍然很陌生,我无法完全正确

here is the template: 这是模板:

#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

The constructor takes a pointer to a (get)method of the class, a pointer to a comparison operator acting on values returned by that get method and finaly some data that it should use for comparisons 构造函数使用一个指向类(获取)方法的指针,一个指向比较运算符的指针,该比较运算符作用于该get方法返回的值,并最终确定一些用于比较的数据

Im trying to use the template in the following way: 我试图通过以下方式使用模板:

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);

i can compile just fine when im not using the function part of the function object but when i try to apply it to i i get an error: 当我不使用功能对象的功能部分时,我可以编译的很好,但是当我尝试将其应用于i ,出现错误:

Must use '.*' or '->*' to call pointer to member function in Member...
bool operator()(Class * other){ return *(this->operation)(other->*(this->method)(), this->data);}

And here the marker is under the arrow other-> I've tried chaing the arrows to points and to move them around a bit at random but cant get it to work. 在这里,标记位于other->箭头下方- other->我尝试过将箭头指向各个点,并随意将它们移动一点,但无法使其正常工作。

As i said im wuite new to this and it might very well be that this is a very strange way to go about things. 正如我所说的那样,这很新,这很可能是处理问题的一种非常奇怪的方式。 if thats the case pointers in the right direction are very welcome, but i would still like to grasp this part. 如果多数民众赞成在正确方向的情况下,指针是非常受欢迎的,但我仍然想掌握这一部分。

Finally i have a bonus question, I will want to make the get-method const after i get this part working, but im not sure how, any help here would also be nice. 最后,我有一个额外的问题,我要在此部分工作后使get-method const生效,但是我不确定如何进行操作,这里的任何帮助也都很好。

Thank you 谢谢

*(this->operation)(other->*(this->method)(), this->data);
^                         ^

Because of operator precedence, the call operator () is invoked before the function pointer is dereferenced. 由于运算符的优先级,在取消引用函数指针之前,将调用调用operator () It will only be dereferenced after the call operator is invoked. 仅在调用调用运算符后才取消引用。

Change it to this: 更改为此:

(*this->operation)((other->*(this->method))(), this->data);
 ^                         ^

Notice that I also put another pair of parenthesis around other->*(this->method)() . 请注意,我还在other->*(this->method)()周围加上了另一对括号。 This is needed because (this->method)() would have been evaluated before other->* . 这是必需的,因为(this->method)()应该在other- other->*之前进行求值。


Also, as @dyp says, you leave out most of the this qualifiers, making the result: 另外,正如@dyp所说,您将忽略大多数this限定符,从而得出结果:

operation((other->*method)(), data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM