Code. Books. Outdoors.

Caution when using operators for overloading and conversion

I have a class that defines several math operations and conversion between other types that was giving unexpected results. After quite a while of debugging, I found that the problem was with the way the compiler casts operands when using operators. My original class was similar to this:

without removing the + and * operators with decimal and int, and it indeed gave the right result. I don’t plan on using this method, as removing the decimal and int operators removes possible other options from the compiler. Plus, it is probably clearer to anyone else reviewing the code and it’s easier to code because there’s only one place where the operations are done.)