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 8014491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:57:05+00:00 2026-06-04T19:57:05+00:00

Given the following example: class BaseClass { BaseClass() { }; virtual ~BaseClass() { this->Cleanup();

  • 0

Given the following example:

class BaseClass
{
  BaseClass()
  {
  };

  virtual ~BaseClass()
  {
    this->Cleanup();
  };

  virtual void Cleanup()
  {
    // Do cleanup here.
  };
};

class Level1DerivedClass : public BaseClass
{
  Level1DerivedClass()
  {
  };

  virtual ~Level1DerivedClass()
  {
  };

  virtual void Cleanup()
  {
    // Call my base cleanup.
    BaseClass::Cleanup();

    // Do additional cleanup here.
  };
};

class Level2DerivedClass : public Level1DerivedClass
{
  Level2DerivedClass()
  {
  };

  ~Level2DerivedClass()
  {
  };

  void Cleanup()
  {
    // Call my base cleanup.
    Level1DerivedClass::Cleanup();

    // Do additional cleanup here.
  };  
};


main()
{
  Level2DerivedClass * derived2 = new Level2DerivedClass();
  delete derived2;
  return 0;
}

When I delete my derived class reference, I would EXPECT the flow would be as follows:

  1. Level2DerivedClass destructor is executed.
  2. Because Level1DerivedClass destructor is virtual, it would be executed.
  3. Because BaseClass destructor is virtual, it would be executed.
  4. Because BaseClass::Cleanup and Level1DerivedClass::Cleanup are both virtual, the call from the BaseClass ‘this’ pointer in the BaseClass destructor would executed the implementation of the most derived class – Level2DerivedClass::Cleanup.
  5. Level2DerivedClass::Cleanup calls its parent’s Cleanup implementation.
  6. Level1DerivedClass::Cleanup calls its parent’s Cleanup implementation.

What is happening is that it is calling the destructors for each level of inheritance (1 – 3) above the way I’m expecting. But when this->Cleanup() is called from the BaseClass destructor, it only executes its own implementation. I don’t understand why this is happening because normally when you instantiate a derived class pointer, cast it as a base class pointer, and call a virtual method from the base class pointer (in this case, ‘this’), it still runs the derived class implementation (the whole point of ‘virtual’, yes?). In my example, Level2DerivedClass::Cleanup and Level1DerivedClass::Cleanup never gets called.

The reason I’m setting it up this way is I want to be able to call my Cleanup code without having to destroy my object, which is why I’m abstracting it from the actual destructor body.

If you have suggestions on a more proper way to do this, I’m all ears. But I would also like an explanation of why my setup doesn’t work – what am I misunderstanding?

Thank you in advance for your time.

  • 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-06-04T19:57:07+00:00Added an answer on June 4, 2026 at 7:57 pm

    The rule of thumb is: Never Call Virtual Functions during Construction or Destruction.

    They don’t behave as you might expect; as each destructor finishes, the dynamic type of this is effectively modified. From [class.cdtor] in the C++ standard:

    When a virtual function is called directly or indirectly from a constructor (including the mem-initializer or
    brace-or-equal-initializer for a non-static data member) or from a destructor, and the object to which the
    call applies is the object under construction or destruction, the function called is the one defined in the
    constructor or destructor’s own class or in one of its bases, but not a function overriding it in a class derived
    from the constructor or destructor’s class, or overriding it in one of the other base classes of the most derived
    object.

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

Sidebar

Related Questions

Given the following block of code: class BaseClass { public: virtual void hello() {
Given the following example classes in my.package ... public class Foo { public void
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following example class: public class MyClass { public string S { get;
I'm having trouble using generics. Given the following example : class A<T> { public
assume the following class is given: class Base{ public: Base() {} Base( const Base&
Background here . In the above link, the following example is given: class <<
Given the following code: [example.h] class MyClass { public: MyClass(); std::string name; std::string address;
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Consider the following sample code: #include <iostream> using namespace std; class base { public:

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.