[英]Limit using/typedef to class scope
I need to store a function prototype inside of a class somehow, but it automatically becomes a global when I try to do so.我需要以某种方式将 function 原型存储在 class 内,但是当我尝试这样做时它会自动变为全局。
The prototype is given trough a template, but I cannot use it straight out of there as I use it inside of thread member function which has to be static (Because otherwise, I break the callback prototype because of the this
pointer).原型是通过模板给出的,但我不能直接使用它,因为我在线程成员 function 内部使用它,它必须是 static (否则,我会因为this
指针而破坏回调原型)。
I cannot have it go global, as I would need to run multiple instances of the class and the prototypes would get jumbled up.我不能让它 go 全局,因为我需要运行 class 的多个实例,并且原型会变得混乱。
Is there a way to do this?有没有办法做到这一点?
template<class proto>
class cl
{
private:
using m_proto = proto;
void* addr;
public:
static void thread(void* p)
{
// p receives a pointer to the current class
cl* clp = (cl*)p;
((m_proto)clp->addr)();
}
};
You can use您可以使用
void (*addr)();
This declares addr
as a member variable of type void (*)()
.这将addr
声明为void (*)()
类型的成员变量。
You can make it more readable by using.您可以通过使用使其更具可读性。
using my_ptr_type = void (*)();
my_ptr_type addr;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.