简体   繁体   English

在这种情况下,如何在C ++中正确传递成员函数作为参数?

[英]How to properly pass member function as argument in this situation in C++?

I want to pass a member function of my C++ class to another member function of the same class. 我想将我的C ++类的成员函数传递给同一类的另一个成员函数。 I did some research and found these similar questions on SO. 我做了一些研究,发现了类似的问题。

Passing a member function as an argument in C++ 在C ++中将成员函数作为参数传递

Function pointer to member function 指向成员函数的函数指针

They don't cover my specific case in an identical manner, but I wrote my code and would think that I adjusted the right parts to make it work in my situation. 他们没有以相同的方式涵盖我的特定案例,但是我编写了代码,并认为我调整了正确的部分以使其在我的情况下可以正常工作。 However, the compiler seems to disagree with me on that... 但是,编译器在这方面似乎与我不同意...

I have the following setup in my C++ class: 我的C ++类中具有以下设置:

CutDetector.h CutDetector.h

class CutDetector {
   double thresholdForFrameIndex(int frameIndex, vector<double> diffs, int steps, double (CutDetector::*thresholdFunction)(vector<double>diffs)); // should take other functions as args
   double calcMean(vector<double> diffs); // should be passed as argument
   double calcMeanMinMax(vector<double> diffs); // should be passed as argument
   double calcMedian(vector<double> diffs); // should be passed as argument
}

CutDetector.h CutDetector.h

double thresholdForFrameIndex(int frameIndex, vector<double> diffs, int steps, double (CutDetector::*thresholdFunction)(vector<double>diffs)) {
    vector<double> window = ... init the window vector ;
    double threshold = thresholdFunction(window);
    return threshold;
}

However, passing the thresholdFunction as an argument like this doesn't work. 但是,将thresholdFunction作为这样的参数传递是行不通的。 The compiler complains with the following error: 编译器抱怨以下错误:

error : called object type 'double (CutDetector::*)(vector<double>)' is not a function or function pointer 错误 :称为对象类型'double (CutDetector::*)(vector<double>)'不是函数或函数指针

Can anyone see why my setup doesn't work and suggest how I can make it so that it works? 谁能看到为什么我的设置不起作用,并提出如何使它起作用的建议? Basically what I want is to be able to pass any member function that calculates a threshold (ie calcMean , calcMeanMinMax , calcMedian ) to the other member function thresholdForFrameIndex . 基本上,我想要的是能够将任何计算阈值的成员函数(即calcMeancalcMeanMinMaxcalcMedian )传递给其他成员函数thresholdForFrameIndex

To invoke a pointer to member function, you need to supply an object: 要调用成员函数的指针,您需要提供一个对象:

double threshold = (this->*thresholdFunction)(window);
                   ^^^^^^^^                 ^

You can't call a member function without an instance of the class. 没有类的实例,就不能调用成员函数。 You need to do something like this: 您需要执行以下操作:

CutDetector cd;
double threshold = (cd.*thresholdFunction)(window);

Or if you have a CutDetector pointer somewhere: 或者,如果您在某处有一个CutDetector指针:

double threshold = (pcd->*thresholdFunction)(window);

Or if thresholdForFrameIndex is a member function: 或者,如果thresholdForFrameIndex是成员函数:

double threshold = (this->*thresholdFunction)(window);

I think it would be easier for you here to make calcMean , calcMeanMinMax and calcMedian static functions and treat like all others non-member functions. 我认为在这里使calcMeancalcMeanMinMaxcalcMedian 静态函数与将其与所有其他非成员函数一样对待会更容易。 Others answers are correct, but in your case i guess that would be better for class design. 其他答案是正确的,但对于您的情况,我想这对于班级设计会更好。

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

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