Is this C++ structure initialization trick safe?

前端 未结 16 1188
有刺的猬
有刺的猬 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:41

    This would make me feel much safer as it should work even if there is a vtable (or the compiler will scream).

    memset(static_cast(this), 0, sizeof(MY_STRUCT));
    

    I'm sure your solution will work, but I doubt there are any guarantees to be made when mixing memset and classes.

提交回复
热议问题