类方法指向其他类的其他方法

心灵雷普

我想知道是否有可能使一个类的方法指向其他类的另一个方法:

考虑一下:

// Class Foo:

class Foo
{
    static int GetA(int a);
    static int GetB(int b);
};



int Foo::GetA(int a)
{
    return a * 2;
}

int Foo::GetB(int b)
{
    return a * 4;
}

// Hooking class methods:

class HookFoo
{
    static int HookGetA(int);
    static int HookGetB(int);
};

int(HookFoo::*HookGetA)(int) = (int(HookFoo::*)(int))0x0; // (0x0 Memory address) or for example: &Foo::GetA;
int(HookFoo::*HookGetB)(int) = (int(HookFoo::*)(int))0x0; // (0x0 Memory address) or for example: &Foo::GetA;

我知道可以这样做:

int(*NewHook)(int) = &Foo::GetA;

但是如何将方法声明为类呢?

安德烈亚斯(Andreas DM)

您可以使用函数指针。
前任:

class A {
public:
    static void say_hello() { cout << "Hello\n"; }
};

class B {
public:
    static void(*hook)();
};
void(*B::hook)() = A::say_hello;

int main()
{   
    B::hook();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章