对于POD结构,可以新的demo_class [const_number]()
确保动态分配的结构是很好的初始化(即不是垃圾) in C ++ 11,然后 < < < /em>?
值得赞赏的是,如果某人对详细规则进行了有关初始化吊舱结构阵列的详细规则。
有关于基本类型的帖子,例如 new Int {}
等。但是关于POD结构没有直接答案。
更重要的是,大多数帖子都没有提及C ++ 11和之后。
更新:
感谢Eerorika的回答。
根据所述答案,该答案说[强调我的]:
to-initialize t type t type的对象表示:
否则, 对象为零 。
。
如何完全理解这一点?结构\类可能具有许多具有不同类型的成员变量。这是否意味着每个成员变量都将是对象零始于零?例如:
struct Point
{
int x;inty;
};
struct Demo
{
Point pt;
double* ptr;
std::string str;
};.
For POD structure, could new Demo_Class[CONST_NUMBER]()
guarantee the dynamically allocated structures are well initialised(i.e. not garbage) in C++11 and afterwards?
It would be appreciated that if somebody shed the light on the detailed rules about the initialization the POD structure array.
There are posts about base types, say new int{}
and etc. But there is no direct answer about POD structures.
What more, most posts does not mention C++11 and afterwards at all.
UPDATE:
Thanks to eerorika's for the repid answer.
As per the said answer, which says that[emphasise mine]:
To value-initialize an object of type T means:
otherwise, the object is zero-initialized.
How to fully understand that? The struct\class may has many member variables with different types. Does it mean every member variable would be the object is zero-initialized? For example:
struct Point
{
int x;inty;
};
struct Demo
{
Point pt;
double* ptr;
std::string str;
};.
发布评论
评论(1)
至少从C ++ 03引入值初始化(可能还通过较旧的默认初始化规则之前),至少已保证它。在C ++ 11或之后,保证尚未删除。
最新草案中的标准报价:
PS避免使用分配
新
。考虑使用std :: vector
。It has been guaranteed at least since C++03 when value initialisation was introduced (probably also before through older default initialisation rules). The guarantee hasn't been removed in C++11 nor afterwards.
Standard quotes from latest draft:
P.S. Avoid using allocating
new
. Consider usingstd::vector
instead.