Let's say I have an alias for vector:
typedef std::vector<double> PlanetData;
And I want it's fields to be accessible via some keys:
double x = planet_data[PlanetDataKeys::PosX]; //planet_data is of type PlanetData
How can I achieve it?
I can define an enum inside a namespace:
namespace PlanetDataKeys {
enum {
PosX = 0,
PosY = 1
};
}
But enum class
is safer:
enum class PlanetDataKeys {
PosX = 0,
PosY = 1
};
However, as implicit casting of enum class
to int
type is disabled, this would require to write:
double x = planet_data[static_cast<int>(PlanetDataKeys::PosX)];
which is a bit awkward .
Which approach is better in this case and why?
Aucun commentaire:
Enregistrer un commentaire