在没有参数的构造函数上没有括号是语言标准吗? [英] Is no parentheses on a constructor with no arguments a language standard?
问题描述
我使用g ++在Cygwin中编译了一个C ++程序,我有一个类的构造函数没有参数。我有行:
I was compiling a C++ program in Cygwin using g++ and I had a class whose constructor had no arguments. I had the lines:
MyClass myObj();
myObj.function1();
当试图编译它时,我收到以下消息:
And when trying to compile it, I got the message:
错误:请求'myObj'中的成员'function1',它是非类类型'MyClass()()()'
error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()'
小的研究,我发现修复是将第一行改为
MyClass myObj;
After a little research, I found that the fix was to change that first line to MyClass myObj;
我可以发誓我已经做了空的构造函数声明与括号C ++之前。这可能是我使用的编译器的一个限制,或者语言标准真的说不使用没有参数的构造函数的圆括号。
I could swear I've done empty constructor declarations with parentheses in C++ before. Is this probably a limitation of the compiler I'm using or does the language standard really say don't use parentheses for a constructor without arguments?
推荐答案
虽然 MyClass myObj();
可以被解析为一个具有空初始化器或函数声明的对象定义,语言标准指定歧义总是在偏好的函数声明。在其他上下文中允许空括号初始化器。在 new
表达式中或构造一个值初始化的临时。
Although MyClass myObj();
could be parsed as an object definition with an empty initializer or a function declaration the language standard specifies that the ambiguity is always resolved in favour of the function declaration. An empty parentheses initializer is allowed in other contexts e.g. in a new
expression or constructing a value-initialized temporary.
这篇关于在没有参数的构造函数上没有括号是语言标准吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!