0

I don't know how to name these things properly. I want to create a class, declare and define it's constructor with a variable and default value. I figured out that both these ways work. But what's the difference? Is one of them correct or both are good?

class Box
{
   public:
   int size;
      Box() {size = 6;};
};


class Box
{
   public:
   int size;
      Box(): size(6) {};
};
100MB
  • 1

1 Answers1

1

There is little difference, for int, but for complex classes, the initializer list is better.

Consider const int, int &, which show the syntactic benefit

mksteve
  • 12,614
  • 3
  • 28
  • 50