Does a c++ struct have a default constructor?

前端 未结 5 1544
野趣味
野趣味 2020-11-28 08:51

I wrote the following code snippet:

void foo()
{
    struct _bar_ 
    {
        int a;
    } bar; 

    cout << \"Value of a is \" << bar.a;
}
<         


        
5条回答
  •  天命终不由人
    2020-11-28 09:07

    Do not rely on this functionality it is non-standard

    just add

    foo() : a() {}
    

    I can't remember the exact state of gcc 4.2 (i think it is too old) but if you were using C++11 you can do the following

    foo()=default;
    

提交回复
热议问题