我想知道是否有可能获得指向类的方法指针的指针,我尝试过这样的事情,考虑一下:
class x {
public:
int a;
int GetSomeVal(int b) {
return b * 4;
}
};
class y {
public:
int ha;
int (*HGetSomeVal)(int); // pointer method
};
// y::HGetSomeVal now points to x::GetSomeVal
int(y::*HGetSomeVal)(int) = (int(y::*)(int))&x::GetSomeVal;
在此上下文中使用:
int main() {
x a;
y* b;
b = (y*)&a; // pointer of x to y
std::function<int(int)> NewFunc;
std::function<int(int)> OtherNewFunc;
NewFunc = std::bind1st(std::mem_fun(&x::GetSomeVal), b); // works!
OtherNewFunc = std::bind1st(std::mem_func(&y::*HGetSomeVal), b); // not get the pointer, i want get the pointer that points to x::GetSomeVal through of y::HGetSomeVal.
// print testing pointers
printf("%p | %p", &x::GetSomeVal, &y::*HGetSomeVal);
// Pointer A: 00421e00 SUCESS, Pointer B: 000000 NOT GET POINTER.
return 0;
}
重复编辑: 这并不重复,我已经跟进了 Function pointer to member function
并且我使用类指针是不一样的,然后我就是 做了这样的事情:
OtherNewFunc = std::bind1st(std::mem_func((y->*(y->HGetSomeVal)), b);
printf("Pointer A: %p | \n", (y->*(y->HGetSomeVal)));
然后我得到了下一个错误:
内部编译器错误:在gimplify_expr中,在gimplify.c:8343
我如何获得指向HGetSomeVal的指针值?