Is this C++ structure initialization trick safe?

前端 未结 16 1069
有刺的猬
有刺的猬 2020-12-23 17:56

Instead of having to remember to initialize a simple \'C\' structure, I might derive from it and zero it in the constructor like this:

struct MY_STRUCT
{
            


        
16条回答
  •  时光说笑
    2020-12-23 18:37

    Much better than a memset, you can use this little trick instead:

    MY_STRUCT foo = { 0 };
    

    This will initialize all members to 0 (or their default value iirc), no need to specifiy a value for each.

提交回复
热议问题