更新时间:2023-02-14 16:56:02
首先,在非静态成员函数中有一个隐含的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.