1

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

3
  • 3
    The only C++11 I see is nullptr. And you can replace BoundingBox() with {} in situations where you need to put something. Here, you actually don't need to put anything there. Commented Jun 18, 2013 at 19:24
  • 1
    Don't perform premature optimization, C++ is fast. How is this looking wrong? Commented Jun 18, 2013 at 19:25
  • 1
    I wasn't sure if it was a new C++11 feature but it apparently is not. Consider me not well versed in C++'s changelog. Commented Jun 18, 2013 at 19:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.