vendredi 2 avril 2021

Which to prefer? An enum class, or a nested unnamed enum type?

enum Color1 { red, blue, green }; // ok
// enum Color2 { red, blue, green }; // error, enum conflicts

struct Color3
{
    enum { red, blue, green }; // ok, no conflicts
};

enum class Color4 { red, blue, green }; // ok, no conflicts
  1. Color1 and Color2 are both weak typing.
  2. Color3 and Color4 are both strong typing.

My questions are:

1. Is there any difference between Color3 and Color4?

2. Which to prefer? Color3 or Color4? Why?

Aucun commentaire:

Enregistrer un commentaire