Howto: c++ Function Pointer with default values
Solution 1
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.
Solution 2
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());
}
Related videos on Youtube
Poul K. Sørensen
https://www.linkedin.com/in/pksorensen/ I can provide you with Azure, D365 and Sharepoint consultants. I work myself with Azure :)
Updated on June 30, 2022Comments
-
Poul K. Sørensen almost 2 years
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());
-
Oliver CharlesworthDefault parameters aren't part of the function signature...
-
-
Poul K. Sørensen about 12 yearsThanks. 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() );
-
Poul K. Sørensen about 12 yearsVent with the suggestion of @pmjordan
-
Matthieu M. about 12 years@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 ?