Перегрузка оператора '++' для типа класса перечисления с ограниченной областью действияC++

Программы на C++. Форум разработчиков
Гость
Перегрузка оператора '++' для типа класса перечисления с ограниченной областью действия

Сообщение Гость »


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?


Источник: https://stackoverflow.com/questions/316 ... class-type

Вернуться в «C++»