jeudi 24 octobre 2019

Default member values or default constructor parameters in structures?

In modern C++ I am allowed to implement a struct with default member values i.e.

struct A
{
  int x = 5;
  float y = 1.0f;
};

but I can also do create a structure that has no "default member values" but can its constructor can be called with default parameters, as in the example below:

struct B {
  int x;
  float y;

  B(int x_ = 5, float y_ = 1.0f) : x(x_), y(y_) {}
};

What I want to know is whether there is any difference from the clean code or architecture point of view between those? Or maybe there are another, even more important differences? In the first case, I've got less amount of code to write and I believe I can still construct the object like A({2, 3.14f}) even if the constructor is not defined.

Which would be your way to go in a project and why?

Aucun commentaire:

Enregistrer un commentaire