Why does this call the default constructor?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:09:48

nothing at all, because X(answer); could be interpreted as the declaration of a variable.

Your answer is hidden in here. If you declare a variable, you invoke its default ctor (if non-POD and all that stuff).

On your edit: To get a temporary, you have a few options:

The parentheses are optional. What you said is identical to X answer;, and it's a declaration statement.

If you want to declare a variable of the type X, you should do it this way:

X y(answer);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!