我需要将此代码转换为 Visual Studio 2008 的函数指针调用。GetValue()
函数可以是虚拟的,但并非总是如此,GetObject()
在不同的上下文中会返回不同的类型。
...
res = GetObject()->GetValue(x, y);
...
调用函数将传入 GetObject()->GetValue
(指向函数的指针 + 调用该函数的对象实例),参数将由被调用函数:
void Foo( (int T::func*)(int, int) )
{
...
res = func(x, y);
...
}
void Bar()
{
Foo( & GetObject()->GetValue );
}
谢谢!
最佳答案
你真的想要std::tr1::function<int(int int)>
为了这份工作。在 MSDN 上查看它的文档,您将是一个快乐的人。
关于c++ - 在 C++ 中将(可能)虚方法作为函数指针传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6496737/