I want to pass a member function of my C++ class to another member function of the same class. I did some research and found these similar questions on SO.
Passing a member function as an argument in 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:
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
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. The compiler complains with the following error:
error: called object type
'double (CutDetector::*)(vector<double>)'
is not a function or function pointer
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 (i.e. calcMean
, calcMeanMinMax
, calcMedian
) to the other member function thresholdForFrameIndex
.