简体   繁体   English

指向void *函数的指针C ++

[英]Pointer to a void * function C++

I am trying to call pointer to a void * function inside the main method and the compiler is saying assigning to 'funcptr<g>' from incompatible type 'void *(void *) . 我试图在main方法中调用指向void *函数的指针,并且编译器说assigning to 'funcptr<g>' from incompatible type 'void *(void *) hello function is actually an argument to pthread_create function. hello函数实际上是pthread_create函数的参数。 That's why it is void * function. 这就是为什么它是void *函数。 How can I create a function pointer to a void * function? 如何创建指向void *函数的函数指针?

#include <iostream> 
#include <pthread.h> 

using namespace std; 

template<typename T> 
using funcptr = void (*T::*)(void *); // I think it is wrong here.

class m { 
public: 
    template <typename T> 
    struct my_struct { 
        funcptr<T> ptr; 
    };
}; 

class g { 
public: 
    static void *hello(void *); 
}; 

int main() { 
    struct m::my_struct<g> h; 
    h.ptr = g::hello; // Error here

    return 0; 
}

How can I create a function pointer to a void * function? 如何创建指向void *函数的函数指针? hello is not a member function, but it's a static function. hello不是成员函数,而是静态函数。

So your funcptr should be as follows: 因此,您的funcptr应该如下所示:

// No template needed.
using funcptr = void* (*)(void *)

Note that hello is declared with static , meaning that it's no longer a member function to g . 请注意, hello是用static声明的, 这意味着它不再是g的成员函数

Static members of a class are not associated with the objects of the class. 类的静态成员与该类的对象关联。

So using void (*T::*)(void *) to cull non-member functions is incorrect. 因此,使用void (*T::*)(void *)来剔除非成员函数是不正确的。

If you're allowed to use a compiler that supports C++11, you don't even need to manually deduct its type anymore, using decltype : 如果允许使用支持C ++ 11的编译器,则甚至不需要使用decltype手动推导其类型:

// decltype deducts its exact type for you.
using funcptr = decltype(&g::hello);

class m 
{ 
public: 
    struct my_struct 
    { 
        funcptr ptr; 
    };
}; 

FYI, since hello does not have its definition, you might encounter a linkage error. 仅供参考,由于hello没有定义,因此您可能会遇到链接错误。 To prevent that, I assumed that there's some implementation inside: 为了防止这种情况,我假设内部有一些实现:

static void *hello(void *) 
{ 
    // Meaningless, but..
    return nullptr;
}

if you're using C++11, you can use std::function<> which just bothers about the return type and parameters of the function and not where they are defined and what are its type. 如果您使用的是C ++ 11,则可以使用std::function<> ,它只是在担心std::function<>的返回类型和参数,而不是它们的定义位置和类型。

Here is the code using std::function<> 这是使用std::function<>的代码

#include <iostream> 
#include <functional>
#include <pthread.h> 

using namespace std; 

class m { 
public: 
    template <typename T> 
    struct my_struct { 
        function<void*(void*)> ptr;
    };
}; 

class g { 
public: 
    static void *hello(void *) {
        cout<<"Hello.."<<endl;
    }
}; 

int main() { 
    struct m::my_struct<g> h; 
    h.ptr = g::hello;
    h.ptr(nullptr);

    return 0; 
}

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

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