uintptr_t _beginthreadex( void *security, // security properties, pass NULL for most times unsigned stack_size, // pass 0 to use a default thread stack size unsigned ( *start_address )( void * ), // pointer to a function where thread starts void *arglist, // the function may needs parameters unsigned initflag, // pass NULL to start the thread right after the _beginthreadex function unsigned *thrdaddr // pointer to an unsigned integer for holding the thread ID );
扯远了,问题1:需要关注的就是那个start_address变量,是一个指向unsigned (*)(void*)函数的指针。对一个类A而言,它的成员函数指针则是unsigned (A::*)(void*)的。
class A {
unsigned Foo(void*) { return 0; }
};
typedef unsigned (*FUNC1)(void*);
typedef unsigned (A::*FUNC2)(void*);
A obj;
unsigned FUNC_WRAPPER(void* p) {
obj.Foo(p);
}
问题2:成员函数的指针的用法。
在知道类成员函数指针不能赋值给全局函数指针之后,想合法地尝试一下:
class A {
public:
unsigned Foo(void*) { return 0; }
void Test(unsigned (A::*pFoo)(void*), void* p) {
pFoo(p); // Compiler Error
}
};
int main() {
A a;
a.Test(A::Foo); // Error
a.Test(&A::Foo); // OK
return 0;
}
而A::Test()函数中的错误就有点麻烦了,需要更正为:
class A {
public:
unsigned Foo(void*) { return 0; }
void Test(unsigned (A::*pFoo)(void*), void* p) {
(this->* pFoo)(p); // inside the class itself, use "this" pointer
}
};
void GlobalTest(unsigned (A::*pFoo)(void*), void* p) {
A a;
(a.* pFoo)(p); // in a global function, use class instance
}
class A {
public:
typedef unsigned (A::*FUNC_PTR)(void*);
unsigned Foo(void*) { return 0; }
void Test(FUNC_PTR pFoo, void* p) {
(this->* pFoo)(p); // inside the class itself, use "this" pointer
}
};
void GlobalTest(A::FUNC_PTR pFoo, void* p) {
A a;
(a.* pFoo)(p); // in a global function, use class instance
}
跪求大大解释一下上面成员函数指针的运行机理orz