c++ - Making prefix operator do nothing on certain circumstances -
overloading postfix operator doesn't work
here program yesterday after fixed it. having trouble negate prefix operator doing if hours , days set @ zero.
numdays numdays::operator--() { --hour; simplify(); return *this; } numdays numdays::operator--(int) { numdays obj1(*this); if(day == 0 && hour > 0) { hour--; } simplify(); return obj1; }
if try use if state postfix operator, both of operators not work if day , hours not @ 0. how make prefix operator nothing if day , hour @ 0?
first, logic seems incorrect, pointed out wimmel. think want disable operator--
when both day,hour
zero.
second, prefix operator should return reference.
third, may have postfix operator use prefix one, don't duplicate code.
all in all:
numdays& numdays::operator--() { if (day > 0 || hour > 0) { --hour; simplify(); } return *this; } numdays numdays::operator--(int) { numdays copy(*this); ++(*this); return copy; }
Comments
Post a Comment