C ++ 11正确的结构初始化 [英] C++11 Proper Structure Initialization
问题描述
在C ++ 11中有这样的结构:
Having a structure like this in C++11:
struct von
{
std::string Name;
unsigned int ID;
std::vector<std::string> Checks;
};
应按如下方式初始化:
von v = {"",0,{}};
或这样:
von v = {};
这两种方法似乎都可以工作,但编译器警告-Wmissing-field-initializers 。
Both ways seem to work, but the compiler warns about -Wmissing-field-initializers on the latter example.
编辑:这里是我的编译器选项:g ++ main.cpp -ansi -Wall -Wextra -Weffc ++ -std = c ++ 0x。我使用g ++(Debian 4.6.2-12)4.6.2
Here are my compiler options: g++ main.cpp -ansi -Wall -Wextra -Weffc++ -std=c++0x. I'm using g++ (Debian 4.6.2-12) 4.6.2
推荐答案
这不需要initializer_list,工作完全精细与C ++ 03。(好的,对于矢量的初始化你需要C ++ 11)在结构或数组初始化,所有没有显式给定的值是零初始化,所以如果这是你想要= {};将工作很好。
This doesn't require initializer_list at all and work perfectly fine with C++03. (Ok, for the initialization of the vector you need C++11) In a struct or array initialization, all not explicitly given values are zero-initialized, so if that's what you want = {}; will work just fine.
这篇关于C ++ 11正确的结构初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!