-
That recursion
So I have this:
Code:
template <typename K, typename V>
void Map<K,V>::print() {
if (!this) {
cout << "THIS PROGRAM SUCKS" << endl;
return;
}
cout << key << " " << value << endl;
left->print();
right->print();
}
And for some reason, I'm printing out the key and value twice when I have a single element. I seriously don't understand why; my logic is that since left and right are both null, the recursive calls to them should return automatically. Also, "THIS PROGRAM SUCKS" is printing out three times as well.
What's wrong with my recursion?
EDIT: Owait, I'm stupid. I forgot 1 return statement, which allowed 2 cases to happen at once, creating a node, and then creating an identical node to the right.
-
Re: That recursion
What is "!this" supposed to do? this is a pointer, not a boolean expression. Also consider making a short, self-contained, compilable example.
Also another note, this will almost never be null. Checking for it is a bit weird. this will always point to the underlying object.
-
Re: That recursion
!this refers to the current map pointer I'm on.
!this = if something's not there