为什么不"foo f();" 调用类"foo"的构造函数?

Nov*_*tor 1 c++ constructor execution

可能重复:
为什么使用一组空括号来调用没有参数的构造函数是错误的?

我碰到了以下问题.我创建了2个foo实例.然后我意识到,foo f();没有执行类的构造函数.这是为什么?

class foo{
public:
    foo() {cout <<"executed contructor...";}
};

int main() {
    foo f(); // doesn't run the ctor???? why?
    foo f2; // this one does execute the ctor


    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

pmr*_*pmr 6

第一个声明了一个函数.尝试访问名为的对象f.编译器会抱怨:f具有非类型类型foo (),这意味着它是一个不带参数的函数并返回一个类型的对象foo.