简体   繁体   English

为什么对象默认初始化但基元不在 C++ 中?

[英]Why are objects default initialized but primitives aren't in C++?

Why are primitives not default initialized but objects are in C++?为什么基元没有默认初始化,但对象在 C++ 中? For example:例如:

class Foo {
  void Method() {
    int w(); // initialized to 0
    int x; // uninitialized
    std::vector<int> y(); // initialized to empty vector
    std::vector<int> z; // initialized to empty vector
  }
}

In this case, w and y are declared with parentheses, and so are initialized, and z is declared without, and gets its no argument default constructor called, but x remains uninitialized.在这种情况下, wy是用括号声明的,因此被初始化, z是没有声明的,并且调用了它的无参数默认构造函数,但x保持未初始化。 Why is this?为什么是这样?

If there is no initializer for an object, the object is default initialized [dcl.init]/12 .如果对象没有初始化器,则对象默认初始化为[dcl.init]/12 If the initializer is () , the object is value initialized [dcl.init]/11 .如果初始化器是() ,则对象是值初始化的[dcl.init]/11 Default initialization of an object of class type (like std::vector<int> ) invokes the default constructor while default initialization for an object of type int means no initialization [dcl.init]/7 .类类型对象(如std::vector<int> )的默认初始化调用默认构造函数,而int类型对象的默认初始化意味着没有初始化[dcl.init]/7 Value initialization will also call the default constructor for objects of class type that have one (like std::vector<int> does), but for objects of type int , value initializtion means zero initialization [dcl.init]/8 .值初始化还将为具有 1 的类类型对象调用默认构造函数(如std::vector<int>所做的那样),但对于int类型的对象,值初始化意味着零初始化[dcl.init]/8 And zero initialization for an int does actually mean that the int is initialized to zero [dcl.init]/6 int零初始化实际上意味着int被初始化为零[dcl.init]/6 ...

As has already been pointed out in the comments,正如评论中已经指出的那样,

int w();

and

std::vector<int> y();

are not in fact definitions of a local variable, but rather declarations of two functions w and y that take no arguments and return an int and an std::vector<int> respectively (the infamous most vexing parse ).实际上不是局部变量的定义,而是两个函数wy声明,它们不带参数并分别返回一个int和一个std::vector<int> (臭名昭著的最令人烦恼的 parse )。 Nevertheless, there are cases where it is possible to use () as an actual initializer, so let's modify your example a bit to demonstrate the behavior you were asking about:尽管如此,在某些情况下可以使用()作为实际的初始化程序,因此让我们稍微修改一下您的示例以演示您所询问的行为:

class Foo
{
    int w;
    std::vector<int> y;

    Foo()
      : w(),  // value initialized == zero initialized for int
        y()   // value initialized == default initialized for std::vector<int>
    {
        int x; // default initialized == uninitialized for int
        std::vector<int> z; // default initialized
    }
};

The "why" here simplifies to "Because C++ was trying to keep performance and behaviors C compatible where it could".这里的“为什么”简化为“因为 C++ 试图尽可能保持 C 的性能和行为兼容”。 When you're selling a new, relatively low-level language that (at least initially) was mostly a superset of C, you don't want to say "If you compile existing C code as C++, it's always slower!"当您销售一种新的、相对低级的语言(至少最初)主要是 C 的超集时,您不会想说“如果您将现有的 C 代码编译为 C++,它总是更慢!” C doesn't zero-initialize primitives by default;默认情况下,C 不会对原语进行零初始化; as long as the code eventually assigns a value to them before reading from them, that's fine, and C++ follows the same pattern.只要代码最终在读取它们之前为它们分配一个值,那很好,C++ 遵循相同的模式。

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

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