[英]Initializing member variables with braces or parentheses
When initializing member variables in a constructor you can do it in these two different ways, using braces or parentheses:在构造函数中初始化成员变量时,您可以通过以下两种不同的方式进行,使用大括号或圆括号:
class some_class {
some_class(): foo{<value>}, bar(<value>) {}
T foo;
T bar;
};
Is there a functional difference between these two initialization methods, and if so, which one should I opt to use?这两种初始化方法之间是否存在功能差异,如果是,我应该选择使用哪一种?
Depends of T
.取决于
T
。 for example with [T = std::vector<int>]
,例如
[T = std::vector<int>]
,
foo{4, 5}
is a container with 2 items (4 and 5), foo{4, 5}
是一个包含 2 个项目(4 和 5)的容器,
whereas foo(42, 0)
is a container with 42 items (with value 0).而
foo(42, 0)
是一个包含 42 个项目(值为 0)的容器。
You might read initialization which explains the different kind of initializations for further detail...您可能会阅读初始化,其中解释了不同类型的初始化以获取更多详细信息...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.