具有不同返回类型的函数指针C [英] Function Pointers with Different Return Types C
问题描述
我了解C中的函数指针以及如何使用它们.但是,我不知道如何拥有一个可以指向具有不同返回类型的函数的函数指针.这可能吗?我知道如何使用函数指针数组,但是我只发现了不同参数的示例,而不是返回类型的示例. ( C :如何对具有可变参数计数的函数使用单个函数指针数组?)如果数组更有意义,您可以提供一个示例来做到这一点吗?
I understand what function pointers are in C as well as how to use them. However, I do not know how to have one function pointer that can point to functions with different return types. Is this possible? I know how to use an array of function pointers, but I have only found examples of different parameters, not return types. (C: How can I use a single function pointer array for functions with variable parameter counts?) If an array would make more sense, can you provide an example of how to do this?
我有一个switch/case语句来选择要指向的功能.然后将其作为参数传递给另一个函数.例如,我在下面列出了一些我需要实现的功能:
I have a switch/case statement to select the function that will be pointed to. This is then passed as an argument to another function. For example, I have listed some of the functions that I will need to implement below:
float* doFloatOps();
void goToSleep();
int* multiplication();
我需要有一个可以设置为指向其中任何一个的函数指针.我认为由于代码的设置,我只能有一个指针.如果您看到一种重组的方法,以使函数指向更容易,我将不胜感激.我的代码的当前结构是这样的:
I need to have one function pointer that can be set to point to either of those. I think that I can only have one pointer due to the setup of the code. If you see a way to restructure this to make the function pointing easier, I would appreciate the input. The current structure of my code is like this:
switch(letter)
{
case 'a':
{
functionPointer = &doFloatOps;
}
case 'b':
{
functionPointer = &goToSleep;
}
.......
}
void carryOutFunctions(functionPointer)
{
while(....)
{
functionPointer;
}
}
请原谅以上所有与函数指针问题无关的语法错误.
Please excuse any syntax errors above not pertaining to the function pointer problem.
所以我的主要问题是:如何要么拥有一组具有不同返回类型的函数指针,要么使一个函数指针指向具有不同返回类型的函数?
So my main question is: how can I either have an array of function pointers with different return types or enable one function pointer to point to functions with different return types?
推荐答案
不可能,您不能拥有指向具有不同返回类型的函数的函数指针,因为它们将是不兼容的类型.在这种情况下,您可以尝试返回void *
,然后根据需要处理返回值.
It's not possible, you can't have a function pointer that points to functions with different return types, because they would be of incompatible types. In this cases you can try returning void *
and then handle the return value according to your needs.
当然,这要求您在处理返回的数据时要非常小心.这种技术的一个例子是在pthread
中,您将具有以下类型void (*)(void *)
的函数指针传递给pthread_create()
.
Of course this requires you to be very careful when handling the returned data. One example of this technique is in the pthread
s where you pass a function pointer to pthread_create()
with the following type void (*)(void *)
.
在您的情况下,如果函数返回void *
应该会起作用,并且请记住void *
可以转换为任何指针类型而无需强制转换.
In your case it should work if the functions return void *
, and remember that void *
is convertible to any pointer type without casting.
您当然也可以使用union
作为返回类型.
You could also use a union
as a return type of course.
这篇关于具有不同返回类型的函数指针C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!