C++ 中默认初始化的原始类型是什么?

What are primitive types default-initialized to in C++?(C++ 中默认初始化的原始类型是什么?)

本文介绍了C++ 中默认初始化的原始类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用初始化列表时:

When I use an initialization list:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

原始类型(intboolfloatenum、指针)成员变量为默认初始化.它获得实现的值是定义的还是所有实现都相同?

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++ 中默认初始化的原始类型是什么?

基础教程推荐