Is this C++ structure initialization trick safe?

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

    My opinion is no. I'm not sure what it gains either.

    As your definition of CMyStruct changes and you add/delete members, this can lead to bugs. Easily.

    Create a constructor for CMyStruct that takes a MyStruct has a parameter.

    CMyStruct::CMyStruct(MyStruct &)
    

    Or something of that sought. You can then initialize a public or private 'MyStruct' member.

提交回复
热议问题