C++ - value of uninitialized vectorlt;intgt;(C++ - 未初始化向量的值lt;intgt;)
问题描述
我从 this question 全局/静态未初始化 int 的值将为 0.这个表示对于向量,将调用对象类型的默认构造函数.
I understand from the answer to this question that values of global/static uninitialized int will be 0. The answer to this one says that for vectors, the default constructor for the object type will be called.
我无法弄清楚 - 当我有 vector<int> 时会发生什么v(10) 在本地函数中.int 的默认构造函数是什么?如果我有 vector<int>v(10) 全局声明?
I am unable to figure out - what happens when I have vector<int> v(10) in a local function. What is the default constructor for int? What if I have vector<int> v(10) declared globally?
我看到的是 vector<int>本地函数中的 v(10) 导致变量为 0 - 但我不确定这是因为我的编译器还是固定的预期行为.
What I am seeing is that vector<int> v(10) in a local function is resulting in variables being 0 - but I am not sure if that is just because of my compiler or is the fixed expected behaviour.
推荐答案
标准中将零初始化指定为内置类型的默认零初始化/值初始化,主要是为了在模板使用中仅支持这种类型的情况.
The zero initialization is specified in the standard as default zero initialization/value initialization for builtin types, primarily to support just this type of case in template use.
请注意,此行为不同于诸如 int x; 之类的局部变量,后者使值未初始化(在 C 语言中,该行为继承自该行为).
Note that this behavior is different from a local variable such as int x; which leaves the value uninitialized (as in the C language that behavior is inherited from).
这篇关于C++ - 未初始化向量的值<int>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - 未初始化向量的值<int>
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
