vendredi 26 février 2021

In what situation would C++11 = default constructor be different from a constructor taking no parameters and an empty body?

I know that if a parametrized constructor is provided, the implicit default constructor is not generated.

If we have a constructor taking no parameters and an empty body it can play the role of a default constructor.

class Box {
public:
  Box(int value) : x(value) {}  // parametrized constructor
  Box() {}  // default constructor
private:
  int x;
};

In C++11 we can write = default to specify that we want the implicitly generated default constructor to be present, even if we have already a parametrized constructor.

class Box {
public:
  Box(int value) : x(value) {}  // parametrized constructor
  Box() = default;
private:
  int x;
};

I am wondering, is there a difference between these two syntaxes for specifying a default constructor explicitly? Are they equivalent or not? Is a constructor taking no parameters and no body really a default constructor or is it something else?

I want to ask, can there be a situation in which the constructor taking no parameters and no body have a different behavior than the C++11 = default constructor? Obscure and arcane examples are welcome.

Aucun commentaire:

Enregistrer un commentaire