简体   繁体   English

这种类型的声明在 c++ 中意味着什么?

[英]what does this type of declaration mean in c++?

I can't understand this;我无法理解这一点; please describe it.请描述一下。 This is C++ code:这是 C++ 代码:

int upper(0), lower(0);

As noted in @dxiv's comment, this is direct initialization of the upper and lower variables.正如@dxiv 的评论中所指出的,这是对上变量和下变量的直接初始化。 The following syntax is also direct initialization:以下语法也是直接初始化:

int even_lower{0};

when applied to non-class type objects.当应用于非类类型对象时。 When applied to objects of class type, it invokes the relevant constructor or conversion operator.当应用于 class 类型的对象时,它会调用相关的构造函数或转换运算符。

I will try to add all possible ways to assign a variable in here with the difference我将尝试添加所有可能的方法来在这里分配变量

1 1

int upper=0, lower=0;
will assign upper as 0 and lower as 0

2 constructor initialization with single value 2 用单值初始化构造函数

int upper(0), lower(0);
will assign upper as 0 and lower as 0

3 constructor initialization with multiple values will initialize a variable with the last value 3 带多个值的构造函数初始化会用最后一个值初始化一个变量

int upper=(0,1,2,3), lower=(44,55,6,77,-5);
will assign upper as 3 and lower as -5

4 Uniform Initialization takes only single value you can't have multiple values in here 4 统一初始化只需要一个值,这里不能有多个值

int upper{0}, lower{0};
will assign upper as 0 and lower as 0

5 you can use = in both constructor and uniform initialization for single values 5 你可以在构造函数和单一值的统一初始化中使用 =

int upper={0}, lower={0};
will assign upper as 0 and lower as 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM