【发布时间】:2017-02-27 05:58:27
【问题描述】:
我想知道是否有可能让一个类的方法指向另一个类的另一个方法:
考虑一下:
// 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;
但是我怎样才能将方法声明到一个类中呢?
【问题讨论】:
-
单纯做
static int HookGetA(int n) { return Foo::GetA(n); }不可行吗?我不清楚你想做什么 -
你不能重新声明/重新分配函数,但你可以在
HookFoo中使用函数指针(如你所演示的) -
不,问题是它是用于内存地址的,我不能这样做,我发布的表格是一个简单的例子。
-
好吧,如果这些是静态函数,它们不需要(不应该)限定在类中,它们只是
int (int)函数