Why does this call the default constructor?

后端 未结 3 1896
萌比男神i
萌比男神i 2020-11-30 19:00
struct X
{
    X()    { std::cout << \"X()\\n\";    }
    X(int) { std::cout << \"X(int)\\n\"; }
};

const int answer = 42;

int main()
{
    X(answer);
         


        
3条回答
  •  情书的邮戳
    2020-11-30 19:35

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

提交回复
热议问题