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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:22+00:00 2026-05-23T09:34:22+00:00

I just read recently that its a good practice to implement C++ destructors as

  • 0

I just read recently that its a good practice to implement C++ destructors as virtual functions[1]. Why is this so? Is this a general good practice? If not, in what conditions/cases is a destructor to be made a virtual function?

Reference

  1. https://www.blackhat.com/presentations/bh-usa-07/Afek/Whitepaper/bh-usa-07-afek-WP.pdf
  • 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-23T09:34:23+00:00Added an answer on May 23, 2026 at 9:34 am

    If a base class has a destructor and it is NOT virtual, then any child class’s destructor will not be called if delete is invoked on the base class’s pointer.

    This can result in memory leaks.

    class Shape
    {
    public:
        Shape()
        {
            cout << "Shape constructor called" << endl;
        }
        // This destructor should be virtual!
        ~Shape()
        {
            cout << "~Shape destructor called" << endl;
        }
    };
    
    class Triangle : public Shape
    {
    public:
        Triangle()
        {
            cout << "Triangle constructor called" << endl;
        }
         ~Triangle()
        {
            cout << "Triangle destructor called" << endl;
        }
    
    }
    
    int main(int argc, char* argv[])
    {
         Shape* pShape = new Triangle();
         cout << "About to call delete" << endl;
         delete pShape;
    }
    

    This will result in:

    Triangle constructor called
    Shape constructor called
    About to call delete
    Shape destructor called

    Any resources that should be deallocated in the triangles destructor has now leaked.

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

Sidebar

Related Questions

I've read the whole Dragon Book recently (just for fun, I'm not really planning
I have done Objective-C way back when, and have recently (i.e. just now) read
I just recently heard of duck typing and I read the Wikipedia article about
I just read the Wikipedia article on mock objects , but I'm still not
I just read this post about why new-line warnings exist, but to be honest
I just read this requirement on a job listing: Aware of the pitfalls of
I just recently read about Mocking objects for unit testing and currently I'm having
I recently read about the fact that there is a possibility of defining getters/setters
I have a pretty large project that just recently started crashing on exit. I
I've been working with UDP sockets quite a lot recently. I've read that UDP

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.