可能重複:
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