I have a tree and each node has an int, lf and rt ptr. I want to reuse traverse() so I can use it to delete and write the int to a file. Below is the code. For the write function, I have to pass in an ofstream, so traverse can write to it. I don't need that for remove_all. Is there a way that I can pass in an empty ofstream from remove_all?
But the problem is that you have one function that does two unrelated things. What ne555 wrote is a good solution. If you find that hard to understand, write two functions traverse_and_delete(PlayerRecord*) and traverse_and_save(PlayerRecord*, ostream&).
Thanks ne555, letting the destructor does the work is great.
hamsterman: In school, the professor always says to group code that have similar function. The traverse_delete and traverse_save both traverse the tree. That is why I want to group them together. In real work environment, is it better to have the 2 separated?
It is good to group similar code, but you should also have your functions do one thing only. If you tried to name your traverse() by what it really does, it would be called traverse_and_save_or_delete(). Those are clearly two unrelated functions. And you're not saving any code this way since you still have to write write_file() and remove_all() functions.