The whole idea is preventing the developer from comparing variables of these two types and from assigning one to another. Of course, if you are trying to break the intended features, you face certain difficulties. And this is good; this is just one of the measures used to prevent breaking the code. Who wants your "blue apple"? I doubt it would be edible. :-)
However, in rare cases you may want to typecast enumerations. Consider this:
class Fruit {
public:
enum Values { apple, banana };
};
class ExtendedFruit {
public:
enum Values {
apple = Fruit::Values::apple, banana = Fruit::Values::banana,
pomelo, quince, plum };
};
This way, you can
safely typecast between
Fruit::Values::apple
and
ExtendedFruit::Values::apple
. The question is: why not having just
enum Fruit { apple, banana };
enum ExtendedFruit { apple = Fruit::apple, banana = Fruit::banana, pomelo };
Oh, this is the ugliness of C++11, which is a direct consequence of extreme ugliness of C++, the need of backward compatibility with older C++, where the enumeration type values could not be repeated in the same namespace, because they used to be used without those
TypeName::
prefixes :-(. Too bad and too late for C++.
—SA