I've been experimenting with the enum class feature of C++ and successfully got the ++ operator to overload as follows:
enum class counter_t : uint8_t {VAL1 = 0, VAL2, VAL3, VAL4, END};
inline counter_t operator ++ (counter_t c, int) {
counter_t c2;
if (c == counter_t::END) {
c2 = counter_t::VAL1;
}
else {
c2 = (counter_t)((uint8_t)c + 1);
}
return (c2);
}
int main(void) {
volatile counter_t x = counter_t::VAL1;
x = x++;
x++;
while(1) {
// Do stuff
}
}
It is fairly straightforward. The "x=x++;" line works fine, however the "x++;" line does not. What is the correct form of the ++ operator function for the autoincrement version?
counter_t cvia reference and modify it++operator only takes one operand, doesn't it? Why do you havecounter_tandint?intargument, so it have a different function signature from the prefix operator.x = x++;still undefined, even ifoperator++is overloaded?x = operator++(x,0);and therefore valid to writex = x++;. Function calls have more sequencing restraints than built-in operators.