Several times I felt the need to define class methods that are called in the same way as constructors or destructors.
A specific example: in the program I had a very complex network of nodes of different types, which depended on each other very irregularly (the network did not resemble a tree at all). When a node needed to be destroyed, he began a complex chain of destruction on the network. Like the web is torn, but harder.
During the execution of this chain, control returned to the initiator's methods (or one of the intermediate elements in the chain), so the actual destruction should have happened when the chain was settled, and therefore I could not use destructors for this purpose. However, according to the class hierarchy of my nodes, I needed a "destructor similar", i.e. The staircase way to call my non-destructive function before destruction (for the same reasons why the so-called actual destructor is also called this way, every step in the class hierarchy should have contributed differently to the chain).
I finished coding the stairs manually. Namely, the nodeBase class has a method called "preDestroyNodeBase", which does its job and calls the virtual method "preDestroyNode", etc. To the sheet (I know that it looks like a constructor, but it was - relatively - more elegant in this way, since you can just call "preDestroy" from the base class).
You can imagine how error-prone this approach is, not to mention ugliness. Is there a cleaner way to emulate a constructor method or a call method destructor? Some kind of magical magic or even magic! Since manual coding is too error prone even for one programmer, so I can’t imagine how this manifests itself for library clients.
, , . , , , .
!