c++ - 类中定义的方法的地址

标签 c++

假设我有一个类:(抱歉另一个问题呃)

class myClass
{
private:
   int a;
public:
   int GetA() { return a; }
};

如果 GetA() 可能通常定义在 cpp int myClass::GetA() { return a; 我可以调试它并找到该函数的地址,但由于它是在类中定义的,所以我不能这样做。

在实际问题中,我想知道 GetDirection() 的地址,但如您所见,它没有任何地址,我尝试搜索调用它的指针,但没有没有任何汇编 call 指令

enter image description here

如果它是在类之外定义的,我会这样做:

int callGetAFunction( DWORD* pointerToThatClass )
{
    int retMe;
    __asm
    {
        mov ecx, pointerToThatClass
        mov eax, 0x00427110 //example address of myClass::GetA()
        call eax
        mov retMe, eax
    }
    return retMe;
}

现在我有了指向那个类的指针,但是没有 GetA() 方法的地址,因为它是用它定义的,那么我该如何调用 GetA() 方法呢? (实际指针是在 dll 上检索的,我正在尝试从那里调用 GetA())

int main()
{
  void* pClass = FunctionThatReturnsAPointerToAMyClassObject();
}

最佳答案

我不确定我明白你的意思。 addressof (&) 运算符怎么样?

int (myClass::*ptr)() = &myClass::GetA;

myClass obj(42); // or however you construct the object
cout << (obj.*ptr)() << endl;

关于c++ - 类中定义的方法的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257749/

相关文章:

c++ - 使用动态规划计算二项式系数

c++ - 如何在 C++ 中比较两个非随机访问迭代器

c++ - 线程 : Fine with 10, 崩溃 10000

c++ - 我应该始终为数字类型使用适当的文字吗?

c++ - 如何与迭代器一起使用?

c++ - FFMPEG实现RTSP客户端,高速播放

c++ - 打印带有千位和百万位分隔符的整数

c++ - 使用 QMathGL 绘制实时数据?

c++ - 在 Linux 上构建 c++ 包时 autogen.sh 的作用是什么

c++ - MySQL PreparedStatement 注意事项