0

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.

Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132
  • 1. Are those *both* really in CutDetector.h, and 2. Did you intentionally omit the `CutDetector::` qualifier on the second snippet definition (i.e. is that a free function and *not* the member declared in the prior class)? – WhozCraig May 07 '15 at 09:19
  • You probably want to pass your vector as a const & to avoid a costly copy operation. – trenki May 07 '15 at 09:22

3 Answers3

2

To invoke a pointer to member function, you need to supply an object:

double threshold = (this->*thresholdFunction)(window);
                   ^^^^^^^^                 ^
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • thanks, that seems like the solution I am looking for! however, when using exactly this syntax the compiler complains again with _error: invalid use of 'this' outside of a non-static member function_ – nburk May 07 '15 at 09:21
  • @nburk well that sort-of answers my second question up in general-comment. You need an instance. Still don't know if that omission of `CutDetector::` was intentional or not. Do you see you defined that function as `double thresholdForFrameIndex(...)` rather than `double CutDetector::thresholdForFrameIndex(...)` ? – WhozCraig May 07 '15 at 09:22
  • ah, you're totally right! this was actually my fault, in my `.cpp`-file I didnt declare the function as being a member of my class... now it works! :) – nburk May 07 '15 at 09:24
2

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:

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

Or if thresholdForFrameIndex is a member function:

double threshold = (this->*thresholdFunction)(window);
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
2

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. Others answers are correct, but in your case i guess that would be better for class design.