Is this C++ structure initialization trick safe?

前端 未结 16 1165
有刺的猬
有刺的猬 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条回答
  •  旧时难觅i
    2020-12-23 18:32

    Comment on litb's answer (seems I'm not yet allowed to comment directly):

    Even with this nice C++-style solution you have to be very careful that you don't apply this naively to a struct containing a non-POD member.

    Some compilers then don't initialize correctly anymore.

    See this answer to a similar question. I personally had the bad experience on VC2008 with an additional std::string.

提交回复
热议问题