Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto: c++ Function Pointer with default values

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()); 
like image 437
Poul K. Sørensen Avatar asked Mar 18 '12 17:03

Poul K. Sørensen


People also ask

CAN default arguments be provided for pointers to functions?

Default argument cannot be provided for pointers to functions.

How do you use a pointer to a function in C?

You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions. For z/OS® XL C/C++, use the __cdecl keyword to declare a pointer to a function as a C linkage.

Can we use pointers for functions C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

What is the correct way to declare a function pointer?

int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);


2 Answers

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.

like image 91
pmdj Avatar answered Sep 16 '22 15:09

pmdj


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());
}
like image 23
Oliver Charlesworth Avatar answered Sep 17 '22 15:09

Oliver Charlesworth