0

I recently ran into some code on a project in an early stage in development. It looks like it's setting default values for a struct, but it's not in any way what I'm used to seeing.

It's not "type name = defaultValue". Instead, it looks more like this...but even then not quite.

It almost looks like a merge of the two. It doesn't seem to complain on compilation aside from warnings...but it baffles me.

struct Point
    {
      Point(double _x = 0, double _y = 0, double _z = 0) :
        x(_x),
        y(_y),
        z(_z) {};
      double x, y, z;
    };

typedef Point Vector;

The typedef also seems suspicious, as it makes relative sense to say "whenever I say point, really underneath it's a vector", but the other way around is...odd. I can only imagine the confusion if someone intended to make a vector but got a point instead.

It's also possible that I did not post enough info for this to be discussed. If so, it'll be another day or so before I can add more details.

Tyler Shellberg
  • 1,086
  • 11
  • 28
  • 1
    What's so weird about the initialization? It's using constructor initialization sequence to initialize the variables instead of doing it in the body of the constructor. This has to do with setting the variables before the instance is fully created. Once the body of the constructor is created, you already have default values for variables, so you will be setting them again. This way, you do it before your instance if fully created. It's very common to use this format, especially when dealing with inheritance. – Everyone May 29 '19 at 05:02
  • `typedef Point Vector;` means that the names `Point` and `Vector` are both names for this struct – M.M May 29 '19 at 05:02
  • @Everyone Huh. I didn't realize that was a thing. What's the advantage of setting the variables before the instance is created? Or doing this in an inheritance setting? (Or in general?) – Tyler Shellberg May 29 '19 at 13:07

0 Answers0