gpt4 book ai didi

c++ - 诠释 x;整数y;诠释*ptr;不是初始化吧?

转载 作者:IT老高 更新时间:2023-10-28 13:00:17 25 4
gpt4 key购买 nike

我正在阅读 J. P. Mueller 和 J. Cogswell 的“C++ All-in-One for Dummies”,偶然发现:

#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
...

This code starts out by initializing all the goodies involved — two integers and a pointer to an integer.

确认一下,这是一个错误,应该读作“...通过声明”,对吗?让我感到奇怪的是,这些基本错误仍然会出现在书籍中。

最佳答案

从语言的角度来看,这是default initialization .问题是,它们被初始化为不确定的值。

otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.

Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)

请注意,任何读取这些不确定值的尝试都会导致 UB .

根据标准,[dcl.init]/7

To default-initialize an object of type T means:

  • If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated([over.match.ctor]), and the best one for the initializer () is chosenthrough overload resolution ([over.match]). The constructor thusselected is called, with an empty argument list, to initialize theobject.

  • If T is an array type, each element is default-initialized.

  • Otherwise, no initialization is performed.

关于c++ - 诠释 x;整数y;诠释*ptr;不是初始化吧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53391694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com