1
任何人都可以解决这个问题吗?我似乎无法在任何地方找到解决方案,但我没有看到为什么下面的行(带有显示编译错误的注释)应该成为问题的逻辑理由。为什么这个指向C++函数代码的指针会产生编译错误?
注:这个问题的How can a C++ base class determine at runtime if a method has been overridden?
class MyClass
{
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”;
}
};
如果你可以做到这一点,C++将有类似[闭包] [1]。 [1]:http://en.wikipedia.org/wiki/Closure_%28computer_science%29 – 2009-11-26 07:43:55
@康拉梅耶http://en.wikipedia.org/wiki/Function_object#In_C_and_C.2B.2B – 2009-11-26 08:05:26