括号成员函数的地址错误 [英] Error with address of parenthesized member function

查看:125
本文介绍了括号成员函数的地址错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些有趣的东西。错误消息说这一切。在获取非静态成员函数的地址时,不允许使用括号的原因是什么?我在gcc 4.3.4上编译它。

I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4.

#include <iostream>

class myfoo{
    public:
     int foo(int number){
         return (number*10);
     }
};

int main (int argc, char * const argv[]) {

    int (myfoo::*fPtr)(int) = NULL;

    fPtr = &(myfoo::foo);  // main.cpp:14

    return 0;

}




错误:main.cpp :14:error:ISO C ++禁止使用非限定或括号非静态成员函数的地址形成指向成员函数的指针。说出& myfoo :: foo'

Error: main.cpp:14: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&myfoo::foo'


推荐答案

您不能使用带括号的表达式的地址。这表明您重写了

From the error message, it looks like you're not allowed to take the address of a parenthesized expression. It's suggesting that you rewrite

fPtr = &(myfoo::foo);  // main.cpp:14

fPtr = &myfoo::foo;

这是由于规范的一部分(&; 5.3.1 / / p>

This is due to a portion of the spec (§5.3.1/3) that reads


指向成员的指针只有在显式& [...]

我的重点)。我不知道为什么这是一个规则(我实际上不知道这到现在),但这似乎是编译器抱怨。

(my emphasis). I'm not sure why this is a rule (and I didn't actually know this until now), but this seems to be what the compiler is complaining about.

希望这有助于!

这篇关于括号成员函数的地址错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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