I am trying out some OpenCV and are on a tutorial for a Canny edge detector Example .
In this tutorial, there is a function declared like this:
void CannyThreshold(int, void*)
and then called like this from inside main;
CannyThreshold(0, 0);
I don't understand the purpose og the (int, void*) part of the declaration since none of these arguments are used in the CannyThreshold funtion.
Why is it simply not just declared like this?
void CannyThreshold();
Note this line in the example:
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
Here, CannyThreshold
is passed as a callback argument to createTrackbar
. The signature of createTrackbar
requires the callback to accept these two arguments, and so CannyThreshold
does, even if it has no use for them.
If you declare the function as
void CannyThreshold();
that is the same as
void CannyThreshold(void);
In other words, it's a function that takes no argument.
If you want the function to take arguments, but never use those arguments, you must still declare the types of the arguments.
This is supposedly because this function is a callback , hence its signature is enforced by whatever API is used (here: OpenCV).
A callback is a name given to a function when this function is registered into an event system to be called later, when something happens. A function registering a callback is called a functor , it takes another function as argument, and do something with it. Exemple:
using callback_t = void(*)(int);
void register_callback(callback_t cb);
If you want to define a callback to be called (back) by register_callback
, it needs to be a function taking an int
and returning void
, even if you don't need the integer. So the following definition is possible:
void my_callback(int) { std::cout << "done\n"; }
register_callback(my_callback);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.