int x {}之间有区别吗?并且int x = 0 ;? [英] Is there a difference between int x{}; and int x = 0;?

查看:640
本文介绍了int x {}之间有区别吗?并且int x = 0 ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 int x {}; 是一种默认初始化,但它与编写 int x = 0完全一样;

I understood that int x{}; is a kind of default-initialization, but is it exactly the same as writing int x = 0; ?

推荐答案

在这种情况下,结果是相同的。

The result is same in this case.


int x {}; 是一种默认初始化

不完全是。请参见默认初始化

int x {}; 值初始化(自C ++ 11起),

int x{}; is value initialization (since C++11),


这是使用空初始化程序构造变量时执行的初始化

This is the initialization performed when a variable is constructed with an empty initializer.

在这种情况下,值初始化的影响(即类类型和数组类型都不是):

And the effects of value initialization in this case (i.e. neither class type nor array type) are:


4)否则,对象将零初始化

最后,在这种情况下,零初始化的效果是:

Finally the effects of zero initialization in this case are:


如果T是标量类型,则对象的初始值为积分
常量零

另一方面, int x = 0; 复制初始化 x 初始化为值 0

On the other hand, int x = 0; is copy initialization; x is initialized with the value 0.

As @PaulR提到,有一个区别,只有c ++ 11支持 int x {}; ,而 int x = 0 没有这样的限制。

As @PaulR mentioned, there's a difference that int x{}; is only supported from c++11, while int x = 0 has not such restriction.

这篇关于int x {}之间有区别吗?并且int x = 0 ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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