Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g++;和Visual Studio_C++_Visual Studio 2008_Pointers_Methods_G++ - Fatal编程技术网

C++ g++;和Visual Studio

C++ g++;和Visual Studio,c++,visual-studio-2008,pointers,methods,g++,C++,Visual Studio 2008,Pointers,Methods,G++,好的,这样做: methodPtrTest.cpp:14: warning: converting from ‘void (Test::*)()’ to ‘void (*)(void*)’ void threadMethod(void*ptr){ 静态_cast(ptr)->testMethod(); } ThreadPtr-ThreadPtr=&threadMethod; 这样,您处理的是一个实函数,而不是PMF。您不能在函数指针和PMF之间转换。只是不。那演员阵容很危险,你不应该想这样做

好的,这样做:

methodPtrTest.cpp:14: warning: converting from ‘void (Test::*)()’ to ‘void (*)(void*)’
void threadMethod(void*ptr){
静态_cast(ptr)->testMethod();
}
ThreadPtr-ThreadPtr=&threadMethod;

这样,您处理的是一个实函数,而不是PMF。

您不能在函数指针和PMF之间转换。只是不。那演员阵容很危险,你不应该想这样做。你到底想解决什么问题?我只是想快速地将一些线程功能破解到一个测试对象中。我并不过分担心安全性,我只是希望编译器不要妨碍我。@Gearoid:至少让你的
testMethod
静态。那么至少您没有在函数指针和PMF之间进行强制转换,或者使用正确的签名生成一个短函数:
void runmemberfn(void*){Test().testMethod();}
methodPtrTest.cpp:14: warning: converting from ‘void (Test::*)()’ to ‘void (*)(void*)’
void threadMethod(void* ptr) {
    static_cast<Test*>(ptr)->testMethod();
}

ThreadPtr threadPtr = &threadMethod;