【发布时间】:2012-01-18 12:54:08
【问题描述】:
可能重复:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?
让我们有这个代码
class Foo {
Foo(int) { }
};
那么我们就有了结果:
int main() {
Foo f1 = Foo(5); // 1: OK, explicit call
Foo f2(5); // 2: OK, implicit call
Foo f3(); // 3: no error, "f3 is a non-class type Foo()", how so?
Foo f4(f1); // 4: OK, implicit call to default copy constructor
Foo f5; // 5: expected error: empty constructor missing
}
你能解释一下3的情况吗?
【问题讨论】:
-
搜索:恼人的解析
-
@Nim:应该是一个答案。
-
另请注意,示例 5 正在执行示例 3 的预期操作并调用默认构造函数。
-
@BjörnPollex,最近,我开始使用适当的搜索词进行评论 - 有时,这只是缺少术语的问题,然后 OP 可以自己发现.. :) 无论如何,我知道有人会更快地给出真正的答案.. ;)
标签: c++ constructor most-vexing-parse