vendredi 19 avril 2019

Are members of a class with defaulted constructors zero-initialized? [duplicate]

This question already has an answer here:

If I have a class:

class MyClass {
   public:
    int value;
};

It's member value won't be zero-initialized if I don't zero-initialize the class:

MyClass c;
std::cout << c.value; // UB: value not initialized.

Does defaulting the default constructor guarantee that members are zero-initialized?

class MyClass {
   public:
    int value;
    MyClass() = default;
};

If I explicitly default the default constructor, will value be initialized to zero now?

MyClass c;
std::cout << c.value; // Guaranteed to print zero?

Note: This question is not a duplicate because I'm asking about the case where the default constructor is explicitly defaulted, and the other question just covers the case where it's not provided.

Aucun commentaire:

Enregistrer un commentaire