Well, the answer to your question about day * 3
is: yes, you can do it. You don't need any operator overloading for that. And the result will be 6
. However, this will work by converting your day
constant to int
type, performing multiplication within the int
type and giving the result of type int
, i.e. that 6
is an int
. Which rises the next question: Are you OK with it being an int
? What are you planning to do with that 6
afterwards? If an int
is OK for your purpose, then you don't need to do anything.
However, it is possible that you actually want to obtain the result of field_type
type from day * 3
. You see, in C++ int
type is not explicitly convertible to enum types. So this will compile and work
int product = day * 3;
but this will not
field_type product = day * 3; // ERROR
You can force the latter to compile by using an explicit cast
field_type product = (field_type) (day * 3);
or you can start playing with operator overloading, like
field_type operator *(field_type lhs, int rhs)
{
return (field_type) ((int) lhs * rhs)
}
Note, that the implementation of the overloaded operator still relies on a cast, so this is just a way to make the "main" code of your program look cleaner, by encapsulating the ugly casts into dedicated operators (nothing's wrong with it).
As a pedantic side note, I'd like to add that there are certain formal dangers in trying to squeeze the results of integer arithmetic operations into a enum type (if that's what you want; maybe you don't, since you seem to be using some other type foo
for the result providing no details about it). The range of values a enum object can represent is [roughly] determined by the maximum (by magnitude) value of enum constant rounded to the next highest (by magnitude) number of the form 2^N-1
. In your case the highest value is day
equal to 2, which means that your enum is guaranteed to represent values up to 3
accurately. If you try to convert 6
to your enum type the result is unspecified (although it will normally work "as expected" in practice).