下面的示例方法旨在检测它是否在派生类中被覆盖。从MSVC得到的错误暗示尝试获取“绑定”成员的函数指针是错误的,但我看不出为什么这会成为问题(毕竟,它将在this->vtable中)。有没有非hacky的方法来修复这个代码?
class MyClass
{
public:
typedef void (MyClass::*MethodPtr)();
virtual void Method()
{
MethodPtr a = &MyClass::Method; // legal
MethodPtr b = &Method; // <<< error C2276: ‘&’ : illegal operation on bound member function expression
if (a == b) // this method has not been overridden?
throw “Not overridden”;
}
};