This is maybe a bit weird question, but I really don't know how to phrase it better than this.
I just discovered that I can do the following:
#include <iostream>
enum class Colour // also works with plain old enum
{
Red = 1,
Green,
Blue,
Yellow,
Black,
White
};
int main()
{
Colour c = Colour(15); // this is the line I don't quite understand
std::cout << static_cast<int>(c) << std::endl; // this returns 15
return 0;
}
So now I have integer value 15 in a variable of the type Colour
.
What exactly is happening here? Is that some kind of enum "constructor" at work? As far as I know, integer value 15 is not put into enumeration, it is stored just in variable c
. Why would something like that be useful, in the first place - to create a value that doesn't exist in enum?