Does a c++ struct have a default constructor?

前端 未结 5 1537
野趣味
野趣味 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:12

    Is it true to say that data members of a struct in c++ are always initialized by default (compared to c)? Or is the observed result just coincidence?

    It is a coincidence.

    Your code invokes Undefined Behavior; unless you explicitly set the members to 0 they can be anything.

提交回复
热议问题