1

Can anyone solve this? I can’t seem to find the solution anywhere, but I see no logical reason why the line below (with the comment showing the compile error) should be a problem.

Note: This question is a derivative of 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”;
        }
};
Community
  • 1
  • 1
AndrewR
  • 10,759
  • 10
  • 45
  • 56

1 Answers1

12

ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. This takes care of name mangling. So what you are trying to do will not work in a standards compliant C++ compiler.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • If you could do this, C++ would have something like [closures][1]. [1]: http://en.wikipedia.org/wiki/Closure_%28computer_science%29 – Conrad Meyer Nov 26 '09 at 07:43