proper recursevely tree cleanup

This commit is contained in:
nemu 2009-05-05 13:04:37 +00:00
parent cba83984ee
commit 97ef877c64

View File

@ -661,23 +661,31 @@ double PTheory::Func(register double t, const PDoubleVector& paramValues, const
/** /**
* <p> Recursively clean up theory * <p> Recursively clean up theory
* *
* If data were a pointer to some data on the heap,
* here, we would call delete on it. If it were a "composed" object,
* its destructor would get called automatically after our own
* destructor, so we would not have to worry about it.
*
* So all we have to clean up is the left and right subchild.
* It turns out that we don't have to check for null pointers;
* C++ automatically ignores a call to delete on a NULL pointer
* (according to the man page, the same is true with malloc() in C)
*
* the COOLEST part is that if right is a non-null pointer,
* the destructor gets called recursively!
*
* \param theo * \param theo
*/ */
void PTheory::CleanUp(PTheory *theo) void PTheory::CleanUp(PTheory *theo)
{ {
if (theo->fMul) { // '*' present if (theo->fMul) { // '*' present
CleanUp(theo->fMul); delete theo->fMul;
if (theo->fAdd) { theo->fMul = 0;
CleanUp(theo->fAdd); }
delete theo;
theo = 0; if (theo->fAdd) {
} delete theo->fAdd;
} else { // '*' NOT present theo->fAdd = 0;
if (theo->fAdd) {
CleanUp(theo->fAdd);
delete theo;
theo = 0;
}
} }
} }