c++ - 指向没有静态方法的指针静态方法

标签 c++ visual-c++

我一直在尝试制作一个指针函数,该函数指向一个执行类似操作的方法(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/

相关文章:

c++ - QNetworkReply 在 DEBUG 模式下破坏程序,并在 ntdll.dll 中出现错误

c++ - 如何在不使用外部库的情况下使用 SDL 绘制线条

c++ - 使用strtok分割字符串

c++ - Visual Studio 无法打开包含文件;没有这样的文件或目录

c++静态多态性(CRTP)在评估 `static constexpr`时导致类型不完整

C++将 vector 保存/加载到文件

c++ - 使用 'typename' 关键字将非类型视为依赖上下文中的类型

c++ - getadapteraddresses 函数是否默认支持mediasense 函数?

c++ - #pragma warning(push) 没有 #pragma warning(pop)

c++ - 一次只能操纵一个 union 成员是什么意思?