开发者

Why does this pointer to C++ function code generate a compile error?

开发者 https://www.devze.com 2022-12-13 01:38 出处:网络
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.

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”;
        }
};


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消