Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:15:05+00:00 2026-05-28T11:15:05+00:00

on more than one occasion I felt the need to define class methods that

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:15:06+00:00Added an answer on May 28, 2026 at 11:15 am

    When you declare a virtual function it will call to the most-derived handler of the function, and then from there you have each handler call NextMostDerivedClass::preDestroy and the calls will go to the next-most-derived handler of the function, and you can again call NextMostDerivedClass::preDestroy, and so on. This is the same as the path a virtual destructor takes, except that you don’t have to call anything manually with destructors, it’s automated. If you were to put the cout statements from the below code sample into your destructors, you could see the same output as the sample below provides.

    #include <iostream.h>
    
    class Foo
    {
        public:
        virtual void PreDestroy()
        {
            cout << "Foo preDestroy";
        }
    }
    
    class Bar : public Foo
    {
        public:
        void PreDestroy()
        {
            cout << "Bar preDestroy\n\n";
    
            Foo::PreDestroy();
        }
    }
    
    class MostDerived : public Bar
    {
        public:
        void PreDestroy()
        {
            cout << "MostDerived preDestroy\n\n";
    
            Bar::PreDestroy();
        }
    }
    
    int main() 
    {
        MostDerived testObj;
    
        testObj.PreDestroy();
    }
    

    Output should be:

    MostDerived preDestroy

    Bar preDestroy

    Foo preDestroy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have methods with more than one parameter that are guarded against bad input
On more than one occasion I have found myself desiring a variable visibility that
I use more than one class and I need a... lets say Global storage
I need to process more than one function that goes and performs results returning
On more than one occasion I've been asked to implement rules for password selection
You can use more than one css class in an HTML tag in current
On more than one occasion I've seen crashing bugs appear on iOS 3.x due
I need to detect more than one \n . Doesn't matter if it's 2
I need to change more than one style attribute for a given element. I
Does more than one instance of a controller get created per App-Domain? If so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.