Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling overriden class methods as a chain in C++
    primarykey
    data
    text
    <p>on more than one occasion I felt the need to define class methods that get called in a manner similar to constructors or destructors. </p> <p>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.</p> <p>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).</p> <p>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).</p> <p>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.</p> <p>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.</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COAfter reading your question, I had no idea what your destructors should actually achieve. The problem is: If you can't explain the semantics of your node network clearly in the context of a SO question (and have to resort to vague expressions like "destructor like"), the design is probably very unclear. If it is unclear to you, it will be a jungle to others. So, can you state clearly in few sentences what function you want realized? If not, consider changing your design to something simpler and less elegant.
      singulars
    2. COThanks for your comment; I'm sorry for having to use vague expressions, but I'm really unable to find a term for it. I've been searching for an answer to this question for a long time now. If you are not interested in the example or the details of why I am asking this question, the question is very simple. "What is the most elegant way of defining functions which call their overriding functions (or the other way round) in a chain across the class hierarchy?". I've used the constructors and the destructors as examples since they are called this way by design.
      singulars
    3. COIf I understand you right, you just need to call a virtual 'preDestroy' function, which will go to the most-derived class, and then just make sure every handler of 'preDestroy' is calling `__super::preDestroy`, this will go from the most-derived to the next most, and so on until it gets to the base class that defined the original virtual method.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload