Saturday, December 23, 2006

prefix vs postfix -- operator

I realized if I have a choice I should use prefix -- instead of postfix.

int findLength(const Node* list) {
int count = 0;
for(const Node* current = list; count++ < current =" current-">getNext()) {
}
return count--; //<-------------- bug
}
This is a bug [which compiler doesn't warn either] - what is the point of incrementing a local variable after it is returned!?

--count here works fine and is correct.

No comments: