【发布时间】:2017-04-01 01:58:48
【问题描述】:
我一直在尝试创建一个指针函数,该函数指向一个执行类似这样的方法的方法(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();
编译器返回我:
错误 1 错误 LNK2001: 外部符号 "public: static int (__stdcall * Dy::pSomeMethod) (void)" (?PSomeMethod@Dy@@2P6GHXZA) 未解决
我不明白,因为不是假设pSomeMethod他没有指向SomeMethod?
【问题讨论】:
-
你不明白函数指针和类方法指针的根本区别。它们是两个完全不同的东西,你不能在两者之间转换。也不能将一个类的类方法指针转换为不同类的类方法指针。
标签: c++ visual-c++