指向 void * 函数的指针 C++
Pointer to a void * function C++
我试图在 main 方法中调用指向 void *
函数的指针,编译器提示 assigning to 'funcptr<g>' from incompatible type 'void *(void *)
。 hello
函数实际上是 pthread_create
函数的参数。这就是为什么它是 void *
函数。如何创建指向 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?
hello
is not a member function, but it's a static function.
所以你的funcptr
应该是这样的:
// No template needed.
using funcptr = void* (*)(void *)
注意 hello
是用 static, meaning that it's no longer a member function to g
.
声明的
Static members of a class are not associated with the objects of the class.
所以使用void (*T::*)(void *)
剔除非成员函数是不正确的。
如果你被允许使用支持 C++11 的编译器,你甚至不需要手动推导它的类型了,使用decltype :
// decltype deducts its exact type for you.
using funcptr = decltype(&g::hello);
class m
{
public:
struct my_struct
{
funcptr ptr;
};
};
仅供参考,由于 hello
没有定义,您可能会遇到链接错误。为了防止这种情况,我假设里面有一些实现:
static void *hello(void *)
{
// Meaningless, but..
return nullptr;
}
如果您使用的是 C++11,则可以使用 std::function<>
,它只关心函数的 return 类型和参数,而不关心它们的定义位置和类型.
这是使用 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;
}
我试图在 main 方法中调用指向 void *
函数的指针,编译器提示 assigning to 'funcptr<g>' from incompatible type 'void *(void *)
。 hello
函数实际上是 pthread_create
函数的参数。这就是为什么它是 void *
函数。如何创建指向 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?
hello
is not a member function, but it's a static function.
所以你的funcptr
应该是这样的:
// No template needed.
using funcptr = void* (*)(void *)
注意 hello
是用 static, meaning that it's no longer a member function to g
.
Static members of a class are not associated with the objects of the class.
所以使用void (*T::*)(void *)
剔除非成员函数是不正确的。
如果你被允许使用支持 C++11 的编译器,你甚至不需要手动推导它的类型了,使用decltype :
// decltype deducts its exact type for you.
using funcptr = decltype(&g::hello);
class m
{
public:
struct my_struct
{
funcptr ptr;
};
};
仅供参考,由于 hello
没有定义,您可能会遇到链接错误。为了防止这种情况,我假设里面有一些实现:
static void *hello(void *)
{
// Meaningless, but..
return nullptr;
}
如果您使用的是 C++11,则可以使用 std::function<>
,它只关心函数的 return 类型和参数,而不关心它们的定义位置和类型.
这是使用 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;
}