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.