为什么这个指向C ++函数代码的指针会产生编译错误? [英] Why does this pointer to C++ function code generate a compile error?

查看:320
本文介绍了为什么这个指向C ++函数代码的指针会产生编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解决这个问题吗?我似乎找不到解决方案在任何地方,但我看不到逻辑原因为什么下面的行(与显示编译错误的评论)应该是一个问题。

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 ++禁止将非限定或括号非静态成员函数形成指向成员函数的指针。这需要注意名称的改动。所以你试图做的不会在符合标准的C ++编译器中工作。

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.

这篇关于为什么这个指向C ++函数代码的指针会产生编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆