我一直在尝试制作一个指针函数,该函数指向一个执行类似操作的方法(Visual C++):
struct test
{
int tst(int a)
{
return a * 4;
}
};
// ok, this the visual C++ compiler does not accept it... (mingw accept it)
int(*tstptr)(int) = (int(*)(int))&test::tst;
然后我做了这样的事情:
struct Dx
{
int SomeMethod()
{
return 4;
}
};
struct Dy
{
static int(*pSomeMethod)();
};
int(Dy::*pSomeMethod)() = (int( Dy::*)())&Dx::SomeMethod;
到目前为止一切顺利,编译没有问题,但如果我试着调用她:
Dy::pSomeMethod();
编译器返回我:
Error 1 error LNK2001: external symbol "public: static int (__stdcall * Dy::pSomeMethod) (void)" (? PSomeMethod@Dy@@2P6GHXZA) unresolved
我不明白,因为不是假设 pSomeMethod
他没有指向 SomeMethod
吗?
最佳答案
&test::tst
的类型是int (test::*) (int)
,是成员函数指针。
您正试图将其转换为常规 指针类型,这是不可能的,因为它们完全不同。
这就是为什么您会遇到此类型转换错误的原因:
error C2440: 'type cast' :
cannot convert from 'int (__thiscall test::* )(int)' to 'int (__cdecl *)(int)'
关于c++ - 指向没有静态方法的指针静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151982/