构造函数在这里被调用了吗?

Sad*_*que 1 c++ visual-c++ c++11

#include<iostream>

class _ctor
{
public:
_ctor() { std::cout<<"\nCtor";}
~_ctor(){ std::cout<<"\nDtor";}
};

_ctor A(); // --> Is the Constructor Really called? I do not see the Output printed
//_ctor A;

int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出在这个链接中给出 我没有看到构造函数被调用,可能是什么问题?如果它不应该被调用那么这意味着_ctor A();什么?

Ben*_*ley 10

您声明了一个函数A(),该函数返回a _ctor,您从未调用该函数.你甚至从未定义过这个功能.

不,没有调用_ctor的构造函数.

  • 请参阅http://stackoverflow.com/questions/180172/why-is-it-an-error-to-use-an-empty-set-of-brackets-to-call-a-constructor-with-no (3认同)

Fre*_*son 5

不,因为你实际上声明了一个不带参数的函数并返回一个_ctor.这被称为"最令人烦恼的解析".你可能想要这个:

_ctor A;
Run Code Online (Sandbox Code Playgroud)