C ++:通过隐式构造函数初始化int变量 [英] C++: initialization of int variables by an implicit constructor

查看:149
本文介绍了C ++:通过隐式构造函数初始化int变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,我对int变量的初始化有点困惑。



这段代码(包括注释)是Nawaz回答的复制/粘贴在本主题中为什么C ++需要用户提供的默认构造函数来默认构造一个const对象?

  struct POD 
{
int i;
};

POD p1; // uninitialized - but do not worry we can assign some value later on!
p1.i = 10; //稍后分配一些值!

POD p2 = POD(); //初始化

对于p2,我知道发生了以下情况:




  • 调用缺省构造函数POD()来创建临时POD对象。构造函数不是用户定义的,因此它是隐式的。对于类似int的内置类型,隐式默认构造函数不执行任何操作(无初始化)。因此, i 包含一些随机内容。

  • 使用临时POD对象调用复制构造函数来创建p2未初始化)。因此,p2的i成员不应被初始化。



但是,注释说p2已初始化!
欢迎任何解释。

解决方案


对于像int这样的内建类型,隐式默认构造函数不执行任何操作初始化)。


这是真的,但它也不是。



为什么这些情况不同?



1。 C ++ 11,8.5 / 11




如果为某个对象指定没有初始化程序 - 初始化;如果未执行初始化,则具有自动或动态存储持续时间的对象具有不确定值


使用 int i; 这将导致未初始化的整数!



2。 C ++ 11,8.5 / 10




一个对象的初始化程序是一个空的圆括号, 如果使用 int i = int();




您有一个值初始化 i 。现在,什么是值初始化?



3。 C ++ 11,8.5 / 7




值初始化 T类型的对象




  • [...](部分选项,其中T可能是类别或阵列类型)
  • $ b

Ok现在我们知道 int i = int(); 表示有 i = 0

由于你的结构体是POD,所以值初始化意味着值初始化所有的成员



您可以有一般行为的快捷方式

  int i1,i2 = int 
std :: cout<< i1<< std :: endl;
std :: cout<< i2<< std :: endl;

如果i1所在的内存不是零,你可以有输出

 somevalue 
0

>正如@jogojapan正确地提到:从 i1 读取在第一位未定义,所以不要这样做。你很可能会观察到我在这里描述的,但因为标准不强制编译器以这种方式行事 i1 可能为零,或制止预期结果在任何其他奇怪]



请注意以下事项:

注意:由于()是初始化语法不允许的,

X a();

不是类X的值初始化对象的声明,而是不带参数并返回X的函数的声明。


强调标准报价是我的。


I am learning C++ and I am a bit confused about the initialization of int variables.

This code (including the comments) is a copy/paste from Nawaz's answer in this topic Why does C++ require a user-provided default constructor to default-construct a const object?

struct POD
{
  int i;
};

POD p1; //uninitialized - but don't worry we can assign some value later on!
p1.i = 10; //assign some value later on!

POD p2 = POD(); //initialized

For p2, I understand that the following is happening:

  • The default constructor POD() is called to create a temporary POD object. The constructor is not user-defined, so it is implicit. For built-in types like int, the implicit default constructor does nothing (no initialization). Therefore i contains some random stuff.
  • The copy constructor is called to create p2 using the temporary POD object (whose i is still uninitialized). Therefore the i member of p2 should not be initialized either.

However, the comment says that p2 is initialized! Any explanation is welcome. Thanks.

解决方案

For built-in types like int, the implicit default constructor does nothing (no initialization).

This is true but it is also not. Default initialization results in an unitialized object, while value initialization doesn't.

Why are these cases different?

1. C++11, 8.5/11

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

If you use int i; this results in an uninitialized integer!

2. C++11, 8.5/10

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

If you use int i = int(); you have a value-initialized i. Now, what is value-initialized?

3. C++11, 8.5/7

To value-initialize an object of type T means:

  • [...] (some options where T may be class or array type)
  • otherwise, the object is zero-initialized.

Ok now we know that int i = int(); means having i=0.

Since your struct is POD, value-initializing it means value-initializing all of its members.

You can have a shortcut on the general behaviour

int i1, i2 = int();
std::cout << i1 << std::endl;
std::cout << i2 << std::endl;

If the memory where i1 resides, isn't zero by any luck you can have the output will probably be

somevalue
0

[As @jogojapan mentioned correctly: Reading from i1 is undefined in first place, so don't do it. You'll most likely observe what I describe here but since the standard doesn't enforce compilers to behave this way i1 may be zero, or brake the expected result in any other strange way.]

Be aware of the following:

Note: Since () is not permitted by the syntax for initializer,

X a();

is not the declaration of a value-initialized object of class X, but the declaration of a function taking no argument and returning an X.

Emphasis on standard quotes are mine.

这篇关于C ++:通过隐式构造函数初始化int变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆