7

I found many articles explaining the difference between "default-initialization and value-initialization" but in fact I didn't understand clearly.

Here's an example:

class A{
   public:
      int x;
};


int main(){
    A a;// default initialization so x has undefined value.
    A b = A(); // value initialization so x is a scalar thus it is value initialized to 0

}

Above it is OK as I guess but here:

int value = 4; // is this considered a value-initialization?

Please help me understand the major differences between the two forms of initializations.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Rami Yen
  • 165
  • 1
  • 6
  • 3
    [Looks like Copy Initialization](https://en.cppreference.com/w/cpp/language/copy_initialization) Here's [another link](https://en.cppreference.com/w/cpp/language/initialization) that (roughly) covers all of the different types of initialization you could run across – user4581301 Jun 27 '19 at 00:37
  • 2
    `A a;` does not have default initialisation. I thought `A a1{};` default initialises, and `A a2{1};` value initialises, and `A a3{a2};` copy initialises, although the above link states that `A a1{};` is value initialised, so perhaps I'm just wrong. – Tas Jun 27 '19 at 00:48
  • 1
    @Tas Yes you're just wrong. `A a1{};` is value initialization, and `A a2{1};` and `A a3{a2};` are direct initialization. – M.M Jun 27 '19 at 05:33

1 Answers1

4

A a; is default initialization, as the effect the default constructor of A is used for initialization. Since the implicitly-generated default constructor of A does nothing, a.x has indeterminate value.

A() is value initialization,

if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;

Note the difference with default initialization, A has an implicitly-defined default constructor, and the object is zero-initialized; so the data memeber x of the temporary object A() will be initialized to 0.

A b = A(); is copy initialization, in concept b is initialized from the temporary object A(), so b.x will be initialized to 0 too. Note that because of copy elision, since C++17 b is guaranteed to be value-initialized directly; the copy/move construction is omitted.

int value = 4; is copy initialization too. value will be initialized to 4.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Do you mean if class `A` has a user-defined constructor then writing `A a;` is not default-initialization? – Rami Yen Jun 27 '19 at 09:48
  • 1
    @RamiYen No, `A a;` is still default initialization. And `A()` is still value initialization. But the effect of value initialization will change to be same as default initialization. Both the effect will be initialize the object by the user-defined default constructor, zero initialization doesn't participate in again. – songyuanyao Jun 27 '19 at 09:52