诠释 x;诠释y; int *ptr;不是初始化吧?
int x; int y; int *ptr; is not initialization, right?
我正在阅读 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.
只是为了确认,这是一个错误,应该读作“...通过声明”,对吗?对我来说很奇怪,这样的基本错误仍然出现在书本上。
是的,你是对的。
你声明和定义这些变量,你没有初始化它们!
PS: What is the difference between a definition and a declaration?
此代码声明和定义三个变量但不初始化它们(它们据说值是 不确定的).
变量声明只需要包含关键字extern
.
从语言的角度来看,这就是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 chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
没错。因此,"dummies"。 :)
我们甚至不能将此归咎于遗留问题;历史上,C 程序员会先声明*一个变量,然后 "initialize" 稍后再对其进行第一次赋值。
但从来没有这样的情况,只是简单地声明一个变量,没有初始化器,被认为是 "initializing" 它。**
所以措辞是错误的。
* 从技术上讲 we're talking about definitions,但是当我们说 "declare a variable" 时,我们几乎总是指定义声明。
** 尽管具有静态存储持续时间的对象在其他任何事情发生之前都会经历自己的零初始化阶段,因此在这种情况下放弃自己的初始化并不是灾难。不过,我们不能声称我们已经初始化了那个对象。
我正在阅读 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.
只是为了确认,这是一个错误,应该读作“...通过声明”,对吗?对我来说很奇怪,这样的基本错误仍然出现在书本上。
是的,你是对的。
你声明和定义这些变量,你没有初始化它们!
PS: What is the difference between a definition and a declaration?
此代码声明和定义三个变量但不初始化它们(它们据说值是 不确定的).
变量声明只需要包含关键字extern
.
从语言的角度来看,这就是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 chosen through overload resolution ([over.match]). The constructor thus selected is called, with an empty argument list, to initialize the object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
没错。因此,"dummies"。 :)
我们甚至不能将此归咎于遗留问题;历史上,C 程序员会先声明*一个变量,然后 "initialize" 稍后再对其进行第一次赋值。
但从来没有这样的情况,只是简单地声明一个变量,没有初始化器,被认为是 "initializing" 它。**
所以措辞是错误的。
* 从技术上讲 we're talking about definitions,但是当我们说 "declare a variable" 时,我们几乎总是指定义声明。
** 尽管具有静态存储持续时间的对象在其他任何事情发生之前都会经历自己的零初始化阶段,因此在这种情况下放弃自己的初始化并不是灾难。不过,我们不能声称我们已经初始化了那个对象。