What are primitive types default-initialized to in C++?(C++ 中默认初始化的原始类型是什么?)
问题描述
当我使用初始化列表时:
When I use an initialization list:
struct Struct {
Struct() : memberVariable() {}
int memberVariable;
};
原始类型(int、bool、float、enum、指针)成员变量为默认初始化.它获得实现的值是定义的还是所有实现都相同?
the primitive type (int, bool, float, enum, pointer) member variable is default-initialied. Is the value it gets implementation defined or is it the same for all implementations?
推荐答案
你说的不对.该对象不是默认初始化的,而是值初始化的.而且它的价值是明确的
You are not correct. The object is not default-initialized but value-initialized. And its value is well-defined
int = 0,
bool = false,
float = 0.0f,
enum = (enum type)0,
pointer = null pointer
pointer to member = null member pointer
请注意,零在任何枚举的值范围内,即使它不包含具有该值的显式枚举数,因此将枚举变量初始化为该值是安全的.
Note that zero is in the range of values for any enumeration, even if it doesn't contain an explicit enumerator with that vaue, so it's safe to initialize an enumeration variable to that value.
特别是对于指向数据成员的指针,实际使用的表示不是全零位.在至少 GCC 和 Clang 使用的所谓 C++ Itanium ABI 中,指向数据成员的指针具有全一位空表示.
In particular for pointer to data members, the representation used in practice is not all-zero bits. In the so-called C++ Itanium ABI used by at least GCC and Clang, pointer to data members have an all-one bits null representation.
这篇关于C++ 中默认初始化的原始类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中默认初始化的原始类型是什么?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
