可能重复:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?C++怪异构造
让我们有这样的代码
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
}
您能否解释一下发生了什么情况?
搜索:vexing解析 – Nim
@Nim:应该是一个答案。 –
另请注意,示例5正在执行期望的示例3,并调用默认的构造函数。 – Joe