[英]Why member-of-pointer at operator() doesn't work?
Why work the function call operator not together with a member-of-pointer operator?为什么函数调用运算符不与指针成员运算符一起使用?
struct F
{
void operator()(){}
};
int main()
{
F * f = new F;
f->(); // Why this doesn't compile?
}
I guess the C++ gods simply thought the syntax was too obscure.我猜 C++ 之神只是认为语法太晦涩了。 The correct syntax is either:
正确的语法是:
f->operator()();
Or:或者:
(*f)()
If the ->
operator isn't overloaded then f->
is roughly equivalent to (*f).
如果
->
运算符未重载,则f->
大致等效于(*f).
, (*f).()
doesn't compile either, you would have to use: ,
(*f).()
也不能编译,你必须使用:
(*f).operator()();
您应该将其称为f->operator()()
或(*f)()
。
I don't think there's any specific reason.我认为没有任何具体原因。
Both ->
and .
->
和.
expect the name of a member to follow, and you haven't provided one.期望跟随成员的姓名,而您尚未提供。
There's just no need to create a special case in the grammar to permit that.没有必要在语法中创建一个特殊情况来允许这样做。 It's complicated enough as it is!
已经够复杂了!
(*f)()
is the way to go. (*f)()
是要走的路。
Sorry if that's brief and unfulfilling, but if you're looking for some deep meaning here I think you're bound to be disappointed!抱歉,如果这很简短且无法令人满意,但如果您在这里寻找一些深刻的含义,我想您一定会感到失望!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.