Nico
Nico

Reputation: 1191

How do I correctly use initialization syntax to initialize a struct?

I would like to ZeroMem a struct using the initialization syntax present in the new C++11. Currently I am doing this:

Mesh::Mesh(void) :
    m_bInitialized(false),
    m_BoundingBox(BoundingBox()), // <-- Is this right???
    m_numVertices(0),
    m_pVertexInfos(nullptr),
    m_pFaceIndices(nullptr),
    m_numFaces(0),
    m_numFacesIndices(0),
    m_materialIndex(0),
    m_faceType(NONE)
{
}

Which seems to do the trick, but it just looks kind of ugly and doesn't feel right. Is there a better way?

I was under the impression that what made this initialization syntax so good is that it somehow automagically initialized the memory block that made up the class without wasting more CPU cycles than it would other wise and having a constructor in the syntax would defeat that purpose.

On that note, if someone can explain to me what makes it so good or link me to an article that explains it, I would appreciate it.

Thanks for reading

Upvotes: 1

Views: 95

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476940

You can just say m_BoundingBox(). This will value-initialize the member, which means default-construct for class types and zero-initialize for scalar types.

Upvotes: 4

Related Questions