简体   繁体   English

为什么指针函数总是具有void返回类型?

[英]why is a pointer function always have void return type?

I have seen many programs with pointer functions that have the void return type. 我见过许多带有指针函数的程序,这些函数具有void返回类型。 What is the reason behind this? 这背后的原因是什么? What is the actual need of a void pointer function? void指针函数的实际需求是什么?

void *print_message_function( void *ptr ){
    int *message,i,j;
    message = (int *) ptr;
    if(*message == 1){
        for(i=0;i<5;i++){ res1 += array[i];
        }
    }

    else if(*message == 2){
        for(i=5;i<10;i++){
            res2 += array[i];
        }
    }
}

If possible, please give a real life example of when you'd use a void pointer function. 如果可能的话,请举一个实际的例子说明何时使用void指针函数。

Your terminology of "pointer function" is confusing. 您的“指针功能”术语令人困惑。 It seems to me that your question is 在我看来,您的问题是

Of all functions that return a pointer to some type, it appears that very many of them are functions that return a pointer to void. 在所有返回指向某种类型的指针的函数中,似乎有很多是返回指向void的指针的函数。 Is there any reason for that? 有什么理由吗?

If that is indeed your question, then -- Yes, in C, a void * can be converted to AnyOtherType * without requiring a cast. 如果确实是您的问题,那么-是的,在C中,可以将void *转换为AnyOtherType *而无需AnyOtherType *转换。 As a result, functions such as qsort that take a pointer to a function ("callbacks" as another poster alluded to), can serve as a "generic" function. 结果,诸如qsort之类的函数采用了指向函数的指针(所谓的“回调”作为另一个暗示),可以用作“通用”函数。 When sorting values of a particular type, you pass your own comparator function which internally knows that it is really not a void * , but YourOwnType * . 在对特定类型的值进行排序时,您将传递自己的比较器函数,该函数内部知道它实际上不是void * ,而是YourOwnType *

A function pointer can have any return type, it is just a pointer to a function, so if a function does not intend to anything it returns a void and so does the function pointer. 函数指针可以具有任何返回类型,它只是指向函数的指针,因此,如果函数不打算执行任何操作,则它返回一个void ,函数指针也将返回。

The most common usage of function pointers is in implementing callbacks to provide an asynchronous mechanism. 函数指针最常见的用法是实现回调以提供异步机制。

An previous answer of mine, explains this in much detail: 我先前的答案对此进行了详细解释:

Callback functions in C/C++/C# C / C ++ / C#中的回调函数

A function pointer can have pretty much any return type. 函数指针几乎可以具有任何返回类型。 Consider this example: 考虑以下示例:

#include "stdafx.h"
#include <iostream>

using namespace std;

// this defines a type called MathOp that is a function pointer returning
// an int, that takes 2 int arguments
typedef int (*MathOp)(int, int);

enum MathOpType
{
    Add = 1,
    Subtract = 2
};

// some generic math operation to add two numbers
int AddOperation(int a, int b)
{
    return a+b;
}

// some generic math operation to subtract two numbers
int SubtractOperation(int a, int b)
{
    return a-b;
}

// function to return a math operation function pointer based on some argument
MathOp GetAMathOp(MathOpType opType)
{
    if (opType == MathOpType::Add)
        return &AddOperation;

    if (opType == MathOpType::Subtract)
        return &SubtractOperation;

    return NULL;
}


int _tmain(int argc, _TCHAR* argv[])
{
    // declare a variable with the type MathOp, which is a function pointer to
    // a function taking two int arguments, and returning an int.
    MathOp op = &AddOperation;
    std::cout << op(2, 3) << std::endl;

    // switch the operation we want to perform by calling our one op variable
    op = &SubtractOperation;
    std::cout << op(2, 3) << std::endl;

    // just an example of using a function that returns a function pointer
    std::cout << GetAMathOp(MathOpType::Subtract)(5, 1) << std::endl;

    std::getchar();

    return 0;
}

The program above prints 5, -1, and then 4. Function pointers can have any return type, and are very powerful. 上面的程序先打印5,-1,然后打印4。函数指针可以具有任何返回类型,并且功能非常强大。

There is no requirement for a function pointer to return a void type. 不需要函数指针返回void类型。 90% of the examples demonstrate function pointers with void types; 90%的示例演示了具有void类型的函数指针; because, it is less information (and hopefully less confusion) to distract the reader from the focus of the pointer to the function. 因为,将信息从读者的注意力转移到该函数的焦点上的信息较少(希望减少混乱)。

This excellent tutorial clearly demonstrates function pointers to functions that return an int type. 这个出色的教程清楚地演示了指向返回int类型的函数的函数指针。

How do you define a pointer function? 您如何定义指针函数? If you mean to say "pointer to a function", it's clearly not that. 如果您要说“指向函数的指针”,显然不是那样。 Instead it is returning a void pointer. 相反,它返回一个空指针。 The advantage of using void pointer is that it is generic in nature and can be converted to the pointer of our requirement. 使用void指针的优点是它本质上是通用的,可以转换为我们要求的指针。

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

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