default-initialized & value-initialized & zero-initialized

來源:互聯網
上載者:User
If the object is a local variable, then the data members will be be default-initialized. The initialization that happens by default depends on the type of the variable. For objects of class type, the class itself says what initializer to use if there is not one specified. For example, we noted that if we do not explicitly initialize a string, then the string is implicitly initialized to be empty. No such implicit initialization happens for local variables of built-in type. Local variables of built-in type that are not explicitly initialized are undefined, which means that the variable's value consists of whatever random garbage already happens to occupy the memory in which the variable is created. It is illegal to do anything to an undefined value except to overwrite it with a valid value. Many implementations do not detect the violations of this rule, and allow access to undefined values. The result is almost always a crash or a wrong result, because whatever is in memory by happenstance is almost never a correct value; often it is a value that is invalid for the type. --------------------------------------------------------------------------------------------If the object is used to initialize a container element, either as a side effect of adding a new element to a map, or as the elements of a container defined to have a given size, then the members will be value-initialized. When we index a map with a key that has not yet been seen, the map automatically creates a new element with that key. That element is value-initialized, which, for simple types such as int, is equivalent to setting the value to zero.  -------------------------------------------------------------------------------------------These rules are slightly complicated, but their essentials are: If an object is of a class type that defines one or more constructors, then the appropriate constructor completely controls initialization of the objects of that class. If an object is of built-in type, then value-initializing it sets it to zero, and default-initializing it gives it an undefined value. Otherwise, the object can be only of a class type that does not define any constructors. In that case, value- or default-initializing the object value- or default-initializes each of its data members. This initialization process will be recursive if any of the data members is of a class type with its own constructor. --------------------------------------------------------------------------------------------C++03 Standard 8.5/5: To zero-initialize an object of type T means: — if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T; — if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized; — if T is a union type, the object’s first named data member is zero-initialized; — if T is an array type, each element is zero-initialized; — if T is a reference type, no initialization is performed. To default-initialize an object of type T means: — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is an array type, each element is default-initialized; — otherwise, the object is zero-initialized. To value-initialize an object of type T means: — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; — if T is an array type, then each element is value-initialized; — otherwise, the object is zero-initialized A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed. If T is a cv-qualified type, the cv-unqualified version of T is used for these definitions of zero-initialization, default-initialization, and value-initialization. --------------------------------------------------------------------------------------------Assignment is not initializationThe key difference stems from two observations: Assignment(operator=) always obliterates a previous value; initialization neve do so. Rather, initialization involves creating a new object and giving it a value at the same time.Initialization happens:* In variable declarations* For function parameters on entry to a function* For the return value of a function on return from the function* In constructor initializers.Assignment happens only when using the = operator in an expression.The distinction between initialization and assignment is important because each one causes different operations to run:* constructors(include copy constructor) always control initialization* the operator = member function always controls assignment.

參考:部分摘自<<Accelerated C++>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.