Is this C++ structure initialization trick safe?

You can simply value-initialize the base, and all its members will be zero’ed out. This is guaranteed

struct MY_STRUCT
{
    int n1;
    int n2;
};

class CMyStruct : public MY_STRUCT
{
public:
    CMyStruct():MY_STRUCT() { }
};

For this to work, there should be no user declared constructor in the base class, like in your example.

No nasty memset for that. It’s not guaranteed that memset works in your code, even though it should work in practice.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)