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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:50:58+00:00 2026-06-05T23:50:58+00:00

I was doing a little experiment with virtual destructors to review – wondering if

  • 0

I was doing a little experiment with virtual destructors to review – wondering if anyone has a simple explanation for the following (using vs 2010):

I Define class hierarchy A-B-C-D, D inherits C, C inherits B, B inherits A, A is the Base;

ran 2 experiments:

First experiment –

A has a virtual Destructor.

B has a non-Virtual Destructor

C has a virtual Destructor

D has a non virtual Destructor

//—————————-

Allocate 4 objects on the heap of type D – Point a pointer of A*, B* and C* at the first 3 – Leave the 4th as a D* for Completeness.
Delete all 4 Pointers.

As I expected, in all 4 instances, the complete destructor chain is executed in reverse order from D down to A, freeing all memory.

Second Experiment –

A has a non-virtual Destructor ** Changed A to non virtual

B has a non-Virtual Destructor

C has a virtual Destructor

D has a non virtual Distructor

Allocate 4 objects on the heap of type D – Point a pointer of A*, B*, and C* at the first 3 – Leave the 4th as a D* for Completeness.

Deleting C* and D* pointers:
the complete destructor chain is executed in reverse order from D down to A, freeing all memory.

Deleting B*:
B and then A Destructor is run (leak)

Deleting A*:
Only A Destructor is run (leak)

Can anyone explain Why this is?

When D type opjects are allocated in experiment 2, its immediate base class (C) has a virtual destructor – doesnt that tell the compiler to track it with a Vptr and know the memory type? REGARDLESS of the reference?

Thanks
Mike

  • 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-05T23:51:00+00:00Added an answer on June 5, 2026 at 11:51 pm

    When D type opjects are allocated in experiment 2, its immediate base class (C) has a virtual destructor – doesnt that tell the compiler to track it with a Vptr and know the memory type? REGARDLESS of the reference?

    No.

    In your second test case, A and B don’t have vptrs/vtables. (And even if they did, a non-virtual member function would still be resolved statically, not dynamically.)

    Put another way, a base class does not “inherit” information (such as whether functions are virtual) from derived classes.

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

Sidebar

Related Questions

I'm doing a little experiment trying to randomly place images inside a div using
I'm doing a little experiment, trying to alternate background colours for nested divs. This
I'm doing a little script to zip multiple folders in multiple zip files following
I am doing a little app with JQuery, using drag-n-drop for the interface. Basically,
I am doing a little learning on php and and html using smarty to
I'm doing a little research in developing a simple socket server that will run
i am doing a little ajax ping pong and was wondering if it is
I'm doing this little program in C++ but in codeblocks appears the following error:
I'm doing a little game, using KineticJS. For the animation of the character movement,
I'm doing a little rails app for a prototype. I started off using a

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.