c样,构造函数和均匀初始化之间有什么区别? [英] What are the differences between c-like, constructor, and uniform initialization?

查看:235
本文介绍了c样,构造函数和均匀初始化之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TTBOMK,有三种方式在C ++中初始化变量。

TTBOMK, There are three ways to initialize a variable in C++.

int x = 0;    // C-like initialization
int x (0);    // Constructor initialization
int x {0};    // Uniform initialization

为C ++ 11提供了统一的初始化,用于初始化不同类型的变量,这在C ++ 03中需要不同的语法。

The uniform initialization was brought on for C++11 to provide a more uniform syntax for initializing different types of variables, which required different syntax in C++03.

c样,构造函数和均匀初始化之间有什么区别?

What are the differences between c-like, constructor, and uniform initialization? And should I always use the uniform initialization?

推荐答案

首先,我建议您看一下以下讲座由Herb Sutter提供,他在其中提供了有关该主题的一些建议。大括号初始化讨论始于 23:00左右

First, I would recommend looking at the following talk by Herb Sutter, in which he gives some recommendations about the subject. The brace-initialization discussion starts at around 23:00.

当你谈论原始数据类型时,所有3产生相同的结果。我个人喜欢坚持使用旧的 int x = 0 语法,但它归结为个人喜好。

When you are talking about primitive data types, all 3 yield the same result. I personally prefer sticking with the old int x = 0 syntax, but it comes down to personal preference.

对于类类型,大括号初始化和旧式构造函数初始化不能完全互换。例如:

For class types, brace initialization and old-school constructor initialization are not completely interchangeable. For example:

vector<int> v (100); // Creates a 100-element vector
vector<int> v {100}; // Creates a 1-element vector, holding the value 100.

这是因为 std :: vector 有一个显式定义 std :: initializer_list 作为其唯一参数的构造函数。请记住

This is because std::vector has a constructor that explicitly defines std::initializer_list as its only argument. Keep in mind that

auto var = {1, 2};

创建 std :: initializer_list var 作为其标识符。

初始化列表的一点是它们提供了一致性,预先可用。例如,如果你要在C ++中初始化一个数组,你可以使用:

The thing about initializer lists is that they provide consistency that is a welcome change from what was available beforehand. For example, if you were to initialize an array in C++, you would use:

int arr[] = {1, 2, 3, 4};

但是,如果你想初始化一个向量< int> 使用相同的元素,您必须:

But, if you wanted to initialize a vector<int> with the same elements, you either had to:


  1. 先初始化上述arr,然后传递 arr arr + 4

  2. 分别创建向量和push_back 。

使用C ++ 11,您只需使用

With C++11, you could just use

vector<int> v = {1, 2, 3, 4}; // Same syntax. Nice! Note that the = is optional

另一个大括号初始化有用的实例是, C ++的最烦琐的解析。从谈话中,假设我们有两个类, origin extents ,其实例可以传递构造另一个对象 rectangle 类型。以下语句:

Another instance in which brace initialization is helpful is that it provides a workaround to C++'s most vexing parse. From the talk, assume that we have two classes, origin and extents, whose instances can be passed to construct another object of type rectangle. The following statement:

rectangle w(origin(), extents());

不允许创建矩形对象使用 origin extents 暂时,因为该语句被解析为函数声明。 Tsk tsk。所以通常你必须这样做:

doesn't allow you to create a rectangle object using origin and extents temporaries, because that statement is parsed as a function declaration. Tsk tsk. So normally, you would have to do:

origin  o;
extents e;
rectangle w(o, e);

使用括号初始化,您可以即时创建它们,

With brace initialization, you can create them on the fly, and

rectangle w {origin(), extents()};

将按预期工作,即传递给重载了 origin 对象作为第一个参数,并将 extents 对象作为第二个参数。

will work as intended, i.e. passed to the constructor which is overloaded with an origin object as it's first argument and an extents object as the second.

规则是对象,使用括号初始化,除非你有一个原因不。

The rule is for objects, use brace initialiation unless you have a reason not to.

这篇关于c样,构造函数和均匀初始化之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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