默认构造函数是否初始化内置类型? [英] Does the default constructor initialize built-in types?

查看:293
本文介绍了默认构造函数是否初始化内置类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认构造函数(由编译器创建)是否初始化内置类型?

Does the default constructor (created by the compiler) initialize built-in-types?

推荐答案

编译器)类的默认构造函数不会初始化内建类型的成员。

Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types.

但是,你必须记住,在某些情况下,该类可以通过其他方式来执行。

However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means. Not by default constructor, nor by constructor at all.

例如,有一个普遍的不正确的信念,对于 C 语法 C()总是调用默认构造函数。但在现实中,语法 C()执行类实例的所谓值初始化。它将仅调用默认构造函数,如果它是用户声明的。 (这是在C ++ 03。在C ++ 98 - 只有当类是非POD)。如果类没有用户声明的构造函数,那么 C()将不会调用编译器提供的默认构造函数,而是将执行一种特殊类型的初始化涉及 C 的构造函数。相反,它将直接赋值 - 初始化类的每个成员。

For example, there's a widespread incorrect belief that for class C the syntax C() always invokes default constructor. In reality though, the syntax C() performs so called value-initialization of the class instance. It will only invoke the default constructor if it is user-declared. (That's in C++03. In C++98 - only if the class is non-POD). If the class has no user-declared constructor, then the C() will not call the compiler-provided default constructor, but rather will perform a special kind of initialization that does not involve the constructor of C at all. Instead, it will directly value-initialize every member of the class. For built-in types it results in zero-initialization.

例如,如果你的类没有用户声明的构造函数。

For example, if your class has no user-declared constructor

class C { 
  int x;
};

那么编译器会隐式提供一个。编译器提供的构造函数不会执行任何操作,这意味着它不会初始化 C :: x

then the compiler will implicitly provide one. The compiler-provided constructor will do nothing, meaning that it will not initialize C::x

C c; // Compiler-provided default constructor is used
// Here `c.x` contains garbage

然而,以下初始化将零初始化 x ,因为它们使用显式() initializer

Nevertheless, the following initializations will zero-initialize x because they use the explicit () initializer

C c = C(); // Does not use default constructor for `C()` part
           // Uses value-initialization feature instead
assert(c.x == 0);

C *pc = new C(); // Does not use default constructor for `C()` part
                 // Uses value-initialization feature instead
assert(pc->x == 0);

()在某些方面在C ++ 98和C ++ 03之间,但不是在这种情况下。对于上面的类 C ,它将是相同的:() C :: x 。

The behavior of () initializer is different in some respects between C++98 and C++03, but not in this case. For the above class C it will be the same: () initializer performs zero initialization of C::x.

在不涉及构造函数的情况下执行的初始化的另一个示例当然是聚合初始化

Another example of initialization that is performed without involving constructor is, of course, aggregate initialization

C c = {}; // Does not use any `C` constructors at all. Same as C c{}; in C++11.
assert(c.x == 0);

C d{}; // C++11 style aggregate initialization.
assert(d.x == 0);

这篇关于默认构造函数是否初始化内置类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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