20

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()); 
4
  • 1
    Default parameters aren't part of the function signature... Commented Mar 18, 2012 at 17:52
  • So i cant achieve what i want ? Commented Mar 18, 2012 at 17:53
  • Not this way. Consider std::function instead Commented Mar 18, 2012 at 17:56
  • FeatureParams & params = FeatureParams() will lead to an error. Commented Mar 18, 2012 at 18:03

2 Answers 2

21

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Good suggestion about the class example.
I created a class wrapper, is it possible to make an operator abstract in the base class.
@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 ?
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.
8

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

2 Comments

Thanks. I did typedef void (*RespExtractor) (const cv::Mat & image, cv::Mat & resp,FeatureParams &); and predict_image(im_in,im_out,create_hough_features,FeatureParams & par=DefaultParams() );
Vent with the suggestion of @pmjordan

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.