I've encountered a problem with "redefineing" destructor. The thing is I have been given a definition of struct that i cannot modify. It is a "leaf" of a tree. Goal is to free only a part of the tree while returning a pointer to the remaining subtree. My idea is to use reference counting. This is the given code:
struct TLeaf {
TLeaf* m_L;
TLeaf* m_R;
~TLeaf(void) {
delete m_L;
delete m_R;
}
}
Is there at least way to avoid calling this destructor? Actually any idea is acceptable. :D
Thank you very much good people of Stack Overflow. :)
You could
nullptr
out the values before you delete the node.delete ptr
whereptr
isnullptr
is safe and does nothingSince you cannot modify the TLeaf, what you can do is to modify the user code.
0) If you are using heap allocation, try
operator delete(p)
instead ofdelete p
.1) If you are using stack allocation, avoid directly using the type
TLeaf
: