C++ 作类型转换的时候遇到问题
#include <stdio.h>
static int foo()
{
printf("hello/n");
return 0;
}
typedef int fnc_t();
int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;
fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux
fnc();
return 0;
}
===gcc 编译结果========================
gcc test.cpp
test.cpp: In function `int main(int, char**)':
test.cpp:16: error: invalid conversion from `int (*)()' to `void*'
test.cpp:18: error: ISO C++ forbids casting between pointer-to-function and pointer-to-object
VC6 可以。。。 有空深究一下。
本文探讨了C++中从函数指针到void*再转换回函数指针的问题,并展示了在不同编译器环境下(如GCC与VC6)的行为差异。
680

被折叠的 条评论
为什么被折叠?



