This means that if an expression with several operators is ambiguous, operators that are “exceeded” will be called before the casting operator, and operators that are “below the cast” will be called after the translation operator. As in 2 + 2 * 2 , multiplication must be performed before addition, the result is 6, but not 8.
Consider the following two classes that can be written off with each other:
class E2; class E1 { public: operator E2(); E1 operator++(int) { std::cout << "E1 Post-inc\n"; return *this; } E1 operator*(const E1&) { std::cout << "E1 * E1\n"; return *this; } }; class E2 { public: operator E1() { std::cout << "Cast E1 -> E2\n"; return E1(); } E2 operator++(int) { std::cout << "E2 Post-inc\n"; return *this; } E2 operator*(const E2&) { std::cout << "E2 * E2\n"; return *this; } }; E1::operator E2() { std::cout << "Cast E2 -> E1\n"; return E2(); }
Then let declare
E1 e1; E2 e2;
and do
(E1)e2++;
Both E1 and E2 have a ++ operator, so what should be done first in this expression: pour or increase? The priority table answers that the increment should be executed first, so the program prints
E2 Post-inc Cast E1 -> E2
Then try a more complex example:
e1 * (E1)e2++;
Here we have 3 actions (in the order they appear in the code): multiplication, cast and increment. What order if the execution will be? The use case table says that the increment is the first, the cast is the second (it is lower than the increment), and the multiplication is the last, since it is lower than both. Thus, the output will be:
E2 Post-inc Cast E1 -> E2 E1 * E1
Note that you can easily reorder operations using parentheses. For example, if we replace e1 * (E1)e2++; on (e1 * (E1)e2)++; , we'll get
Cast E1 -> E2 E1 * E1 E1 Post-inc
source share