我知道这个事情是有效的:
I know this thing works:
void myDisplay() { ... } int main() { ... glutDisplayFunc(myDisplay) ... }所以我试图将myDisplay()函数包含在一个类中。因为我想在将来用不同的类重载它。但是,编译器抱怨说,类型为'void(ClassBlah ::)()'的参数不匹配'void(*)()'的参数,
so I tried to include myDisplay() function to a class that I made. Because I want to overload it in the future with a different class. However, the compiler complains that
'。
这是我尝试做的:
class ClassBlah { .... void myDisplay() .... } ...... int main() { ... ClassBlah blah glutDisplayFunc(blah.myDisplay) ... }有人知道如何解决这个问题吗?非常感谢。
Does anybody knows how to fix this problem? Many thanks.
推荐答案首先,在非静态成员函数中有一个隐含的this指针,将 ClassBlah 中的 void myDisplay()更改为静态。解决这个限制是很尴尬的,这就是为什么C ++ faq lite说不要这样做
Firstly, there is an implicit "this" pointer in non-static member functions, so you'll need to change your void myDisplay() in ClassBlah to be static. It's awkward to work around this limitation, which is why the C++ faq lite says don't do it
然后,您应该能够传递 ClassBlah :: myDisplay 。
Then, you should be able to pass the functions as ClassBlah::myDisplay.
根据您的重载动机(例如,您是否要在运行时或在编译时间执行热交换实现),您可以考虑使用实用程序handler静态类,它包含一个指向你的基类的指针,并通过该类指定责任。
Depending on your motivation for overloading (ie are you going to hotswap implementations in and out at runtime, or only at compile time?) you might consider a utility "handler" static class that contains a pointer to your base class, and delegates responsibility through that.
更多推荐
如何传递一个类方法作为另一个函数的参数在C ++和OpenGL?
发布评论