C++ 带括号的成员函数地址错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7134197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error with address of parenthesized member function
提问by Mahesh
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.
我发现了一些有趣的东西。错误消息说明了一切。在获取非静态成员函数的地址时不允许使用括号的原因是什么?我在 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;
}
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'
错误:main.cpp:14:错误:ISO C++ 禁止将未限定的或带括号的非静态成员函数的地址作为指向成员函数的指针。说 '&myfoo::foo'
采纳答案by templatetypedef
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
to
到
fPtr = &myfoo::foo;
This is due to a portion of the spec (§5.3.1/3) that reads
这是由于规范(第 5.3.1/3 节)的一部分
A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses[...]
仅当使用显式 & 并且其操作数是未括在括号中的限定 ID [...]时,才会形成指向成员的指针[...]
(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.
(我的重点)。我不确定为什么这是一条规则(直到现在我才真正知道这一点),但这似乎是编译器所抱怨的。
Hope this helps!
希望这可以帮助!
回答by Johannes Schaub - litb
Imagine this code:
想象一下这段代码:
struct B { int data; };
struct C { int data; };
struct A : B, C {
void f() {
// error: converting "int B::*" to "int*" ?
int *bData = &B::data;
// OK: a normal pointer
int *bData = &(B::data);
}
};
Without the trick with the parentheses, you would not be able to take a pointer directly to B's data member (you would need base-class casts and games with this
- not nice).
如果没有括号的技巧,您将无法直接获取指向 B 数据成员的指针(您将需要基类转换和带有this
- 不好的游戏)。
From the ARM:
从ARM:
Note that the address-of operator must be explicitly used to get a pointer to member; there is no implicit conversion ... Had there been, we would have an ambiguity in the context of a member function ... For example,
void B::f() { int B::* p = &B::i; // OK p = B::i; // error: B::i is an int p = &i; // error: '&i'means '&this->i' which is an 'int*' int *q = &i; // OK q = B::i; // error: 'B::i is an int q = &B::i; // error: '&B::i' is an 'int B::*' }
请注意,必须显式使用 address-of 运算符来获取指向成员的指针;没有隐式转换......如果有,我们在成员函数的上下文中会有歧义......例如,
void B::f() { int B::* p = &B::i; // OK p = B::i; // error: B::i is an int p = &i; // error: '&i'means '&this->i' which is an 'int*' int *q = &i; // OK q = B::i; // error: 'B::i is an int q = &B::i; // error: '&B::i' is an 'int B::*' }
The IS just kept this pre-Standard concept and explicitly mentioned that parentheses make it so that you don't get a pointer to member.
IS 只是保留了这个 pre-Standard 概念,并明确提到括号使它成为这样,这样你就不会得到指向成员的指针。