-2

Which approach is better to use in C++ to create the type House,

struct Coord {
    double latitude;
    double longitude;
};
struct House {
    string number; // example: 17A
    vector<Coord> coordinates;
    bool isCompletelyStraight;
};

or

using Latitude = double;
using Longitude = double;
using Coord = pair<Latitude, Longitude>;
using Number = string; // example: 17A
using IsCompletelelyStraight = bool;
using House = tuple<Number, vector<Coord>, IsCompletelelyStraight>;

?

Alex Bomb
  • 63
  • 3
  • 3
    Code 1, because code 2 is nonsense (and not really the same) – deviantfan Feb 14 '16 at 19:52
  • #1. I don't like `pair`, it doesn't convey very much in the way of meaning. – Galik Feb 14 '16 at 19:56
  • Objects are good because of encapsulation. The first one encapsulates better. It's far more readable. – duffymo Feb 14 '16 at 19:59
  • 1
    `pair` is **not** a general replacement for a struct that happens to have two items. It's for generic programming where you have to deal with types that aren't known at the time you write the code. Stick with `struct Coord`. – Pete Becker Feb 14 '16 at 22:31

1 Answers1

4

The first approach is alot easier to maintain, that's why I would always go for that. The second approach is like using templates when you don't really have to.

A pair is not a good idea because you would have to use them likecoordinates[i].first, coordinates[i].second' while it's more clean to just have the Coord structure and access a vector element like coordinates[i].latitude, coordinates[i].longitude

What about something like this:

using Latitude = double;
using Longitude = double;

struct Coord
{
    Latitude latitude;
    Longitude longitude;
};

using CoordsContainer = vector<Coord>;

struct House {
    string number; // example: 17A
    CoordsContainer coordinates;
    bool isCompletelyStraight;
};
Jts
  • 3,447
  • 1
  • 11
  • 14