Is it possible to redefine destructor?


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 where ptr is nullptr is safe and does nothing

TLeaf* ltmp = node->m_L; TLeaf* rtmp = node->m_R; node->m_L = nullptr; node->m_R = nullptr; delete node; // now use ltmp and rtmp as you need


Since 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 of delete p.

1) If you are using stack allocation, avoid directly using the type TLeaf:

char leaf[sizeof(TLeaf)]; new (leaf) TLeaf(...); // placement new


  • How to delete all nodes of a Binary Search Tree
  • How to disable debug assertion dialog visual studio or on the output exe
  • mutation observer production infinite loop
  • How can I convert this tuple of tuples into a count of its elements?
  • Python : number of characters in text file
  • How to overcome the effect of navigation bar
  • use grep and awk to transfer data from .srt to .csv/xls
  • How to draw cross background via css which is responsive
  • How can I fill all space of a div 100% with 3 divs with the same size?
  • Angular2 emit event up to the DOM tree
  • Android Chronometer starts and stops but carries on counting when stopped
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Object and struct member access and address offset calculation
  • How to assign byte[] as a pointer in C#
  • What is the purpose of TaskExecutor in spring?
  • How VBA declared Volatility works
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Delete MySQLi record without showing the id in the URL
  • Matplotlib draw Spline from multiple points
  • GridView Sorting works once only
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?