on more than one occasion I felt the need to define class methods that get called in a manner similar to constructors or destructors.
A specific example would be; in a program I had a very complex network of nodes of different types that depended mutually on each other in a very irregular fashion (The network did not resemble a tree at all). When a node needed to be destroyed, it started a complex chain of destructions in the network. Much like a spider web being torn apart, but more complex.
During the execution of this chain, the control came back to the methods of the initiator (or one of the intermediate elements in the chain), so that the actual destruction had to take place when the chain had settled, and that's why I couldn't use destructors for this purpose. However, along the class hierarchy of my nodes, I needed a "destructor like", i.e. a ladder way of calling my non-destructing pre-destruct function (for exactly the same reasons why an actual destructor is also called that way, namely, every step in the class hierarchy needed to contribute to the chain in a different way).
I ended up coding the ladder by hand. Namely, the class nodeBase has a method called "preDestroyNodeBase" which does its job and calls the virtual method "preDestroyNode" and so on until the leaf (I know, this way it looks like a constructor, but it was -comparatively- more elegant that way, since you can just call the "preDestroy" of the most base class).
You can imagine how error prone this approach is, not to mention ugly. Is there a cleaner way of emulating constructor or destructor way of calling methods? Some kind of template magic or even macro magic! Because hand coding it is too error-prone, even for a single programmer, so I cannot imagine exposing this kind of behavior to the clients of a library.
Maybe I'm missing a fundamental programming concept that obsoletes the need for such functions. If that is the case, I'd be glad if you pointed how else that network of nodes example could be handled.
Thanks a lot!