lundi 12 juillet 2021

Zero-initialissation of a variable of class type having a base class with a user-supplies constructor

Given the following code snippet:

struct A
{
  int x,y;
  A() : x(0), y(0) {};
};

struct B : A
{
  int z;
};

int main()
{
  B b1 = B(); // is b1 zero-initialised?
  B b2{}; // is b2 zero-initialised?
  return 0;
}

Are b1 and/or b2 zero-initialised (via value initialisation) or not?

I'm especially confused about b1. What I expect to happen is that the temporary object B() is value-initialised, resulting it to be zero-initialised. But I'm not really sure if struct B fits the rules for zero-initialisation. It all seems to depend on whether the default-constructor of B is user provided or non-trivial.

I'm guessing that B has a non-user-provided non-trivial default constructor, which would result in b1 being default-initialised. Am I correct?

Aucun commentaire:

Enregistrer un commentaire