17

I have:

typedef void (*RespExtractor) (const cv::Mat & image, cv::Mat & resp);

virtual void predict_image(const cv::Mat & src,
            cv::Mat & img_detect,cv::Size patch_size,
            RespExtractor );

void create_hough_features(const cv::Mat & image, cv::Mat & resp, FeatureParams & params =   FeatureParams() );

How would i define the RespExtractor to accept a function with default parameters, such i can call:

predict_image(im_in,im_out,create_hough_features);

I tried following, with no succes:

typedef void (*RespExtractor) (const cv::Mat & image, cv::Mat & resp,FeatureParams params, FeatureParams()); 
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

2 Answers2

18

Function pointers themselves can't have default values. You'll either have to wrap the call via the function pointer in a function that does have default parameters (this could even be a small class that wraps the function pointer and has an operator() with default paremeters), or have different function pointers for the different overloads of your functions.

pmdj
  • 22,018
  • 3
  • 52
  • 103
  • Good suggestion about the class example. – Poul K. Sørensen Mar 18 '12 at 18:05
  • I created a class wrapper, is it possible to make an operator abstract in the base class. – Poul K. Sørensen Mar 18 '12 at 18:28
  • 1
    @s093294: it is, but I would advise against it. I would advise using a regular function if it needs be virtual... the question of course would be, why should it be virtual, since it already dispatches to a function pointer ? – Matthieu M. Mar 18 '12 at 19:29
  • Instead of function pointers i decided to create a simple class with operator() overload and then i just inhered from this class instead and pass that as a parameter instead of a function pointer. i made a virtual method generate, which the operator() calls. Thanks for the help. – Poul K. Sørensen Mar 18 '12 at 23:28
6

Default parameters aren't part of the function signature, so you can't do this directly.

However, you could define a wrapper function for create_hough_features, or just a second overload that only takes two arguments:

void create_hough_features(const cv::Mat & image, cv::Mat & resp, FeatureParams & params) {
    // blah
}

void create_hough_features(const cv::Mat & image, cv::Mat & resp) {
    create_hough_features(image, resp, DefaultParams());
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680