为什么没有调用构造函数?

为什么没有调用构造函数?

问题描述:

此代码的行为与我预期的不一致。

This code doesn't behave how I expect it to.

#include<iostream>
using namespace std;

class Class
{
    Class()
    {
        cout<<"default constructor called";
    }

    ~Class()
    {
        cout<<"destrutor called";
    }
};

int main()
{    
    Class object();
}



我希望输出的默认构造函数称为',但我没有看到任何东西作为输出。问题是什么?

I expected the output 'default constructor called', but I did not see anything as the output. What is the problem?

不是。您的行 Class object(); 声明一个函数。你想要写的是 Class对象;

Nope. Your line Class object(); Declared a function. What you want to write is Class object;

试试看。

您可能也对最烦躁的解析感兴趣(正如其他人所说的)。一个很好的例子是第33页的 Effective STL 第6项。(第12次打印,2009年9月)具体来说在第35页顶部的例子是你做的,它解释了为什么解析器将它作为一个函数声明。

You may also be interested in the most vexing parse (as others have noted). A great example is in Effective STL Item 6 on page 33. (In 12th printing, September 2009.) Specifically the example at the top of page 35 is what you did, and it explains why the parser handles it as a function declaration.