更新时间:2023-11-09 23:26:04
基于注释,如果您不能将foo
的签名更改为除原始函数指针之外的任何内容,那么您就必须这样做像这样的东西:
Based on the comments, if you cannot change the signature of foo
to take anything but a raw function pointer... then you'll have to do something like this:
struct XFunc2Wrapper {
static X* x;
static void func2(int v) {
x->func2(v);
}
};
然后只要将XFunc2Wrapper::x
设置为X
,就执行foo(&XFunc2Wrapper::func2)
.它不必嵌套在结构中,它可以只是一些全局指针,但是嵌套有助于更好地建立代码背后的意图.
And then just do foo(&XFunc2Wrapper::func2)
once you set XFunc2Wrapper::x
to be your X
. It doesn't have to be nested in a struct, it can just be some global pointer, but nesting helps establish the intent behind the code better.
但这绝对是(按照Obvlious上尉的)尝试执行foo(std::function<void(int)> )
之后的不得已的手段.
But this should definitely be last resort after (as per Captain Obvlious) trying to do foo(std::function<void(int)> )
.