简体   繁体   English

“ void SomeFunction(int,void *)”是什么意思?

[英]What does “void SomeFunction(int, void*)” mean?

I am trying out some OpenCV and are on a tutorial for a Canny edge detector Example . 我正在尝试一些OpenCV,并且正在使用Canny边缘检测器Example的教程。

In this tutorial, there is a function declared like this: 在本教程中,有一个这样声明的函数:

void CannyThreshold(int, void*)

and then called like this from inside main; 然后从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. 我不理解声明的(int,void *)部分的目的,因为CannyThreshold函数中没有使用任何这些参数。

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 . 在这里, CannyThreshold作为回调参数传递给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. createTrackbar的签名要求回调函数接受这两个参数,即使CannyThreshold没有用到它们也是如此。

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). 据推测这是因为此函数是一个回调 ,因此其签名是通过使用的任何API(此处为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. 如果要定义一个要由register_callback调用(返回)的回调,则即使不需要整数,它也必须是一个带有int并返回void的函数。 So the following definition is possible: 因此,以下定义是可能的:

void my_callback(int) { std::cout << "done\n"; }
register_callback(my_callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM