如何正确使用初始化语法来初始化结构?(How do I correctly use initialization syntax to initialize a struct?)
我想使用新C ++ 11中的初始化语法ZeroMem一个struct。 目前我这样做:
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(OE) { }这似乎是诀窍,但它看起来有点难看,感觉不对。 有没有更好的办法?
我的印象是,使初始化语法如此优秀的原因在于它以某种方式自动初始化构成类的内存块,而不会浪费更多的CPU周期,而在语法中使用构造函数会失败。
在那个问题上,如果有人能够向我解释是什么让它变得如此美好,或者将我链接到解释它的文章,我将不胜感激。
谢谢阅读
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(OE) { }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 ctructor 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
最满意答案
你可以说m_BoundingBox() 。 这将对成员进行值初始化,这意味着类类型的默认构造和标量类型的零初始化。
You can just say m_BoundingBox(). This will value-initialize the member, which means default-ctruct for class types and zero-initialize for scalar types.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 11 条评论) |
本站网友 链路层劫持怎么解决 | 2分钟前 发表 |
m_numFacesIndices(0) | |
本站网友 四平团购网 | 7分钟前 发表 |
m_materialIndex(0) | |
本站网友 上海深坑酒店 | 7分钟前 发表 |
这意味着类类型的默认构造和标量类型的零初始化 | |
本站网友 我愿意陈晓东 | 23分钟前 发表 |
Mesh(void) | |
本站网友 广东阳江海陵岛 | 19分钟前 发表 |
Mesh(void) | |
本站网友 suv销量 | 15分钟前 发表 |
有没有更好的办法? 我的印象是 | |
本站网友 晶莹的什么 | 29分钟前 发表 |
which means default-ctruct for class types and zero-initialize for scalar types. | |
本站网友 永康租房 | 5分钟前 发表 |
如何正确使用初始化语法来初始化结构?(How do I correctly use initialization syntax to initialize a struct?) 我想使用新C ++ 11中的初始化语法ZeroMem一个struct | |
本站网友 婚纱照短发造型 | 1分钟前 发表 |
或者将我链接到解释它的文章 | |
本站网友 javascript高级程序设计pdf | 16分钟前 发表 |
m_bInitialized(false) |