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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:21+00:00 2026-05-22T02:54:21+00:00

Everyone says that a destructor should be virtual when at least one of class

  • 0

Everyone says that a destructor should be virtual when at least one of class methods is virtual.
My questions is, isn’t it correct to say that a destructor should be virtual when using upcasting ?

class A {
public:

    ~A(){
        cout << "Destructor in A" << endl;
    }
};

class B: public A
{
public:

    ~B(){
        cout << "Destructor in B" << endl;
    }
};

int main()
{
    A* a = new B;
    cout << "Exiting main" << endl;
    delete a;
}

I don’t have any virtual functions in this code, but if I don’t make my base destructor virtual, it will not call B destructor. And yes I know that is pointless to use ucpasting if I don’t have virtual functions.

Thank you.

  • 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-22T02:54:22+00:00Added an answer on May 22, 2026 at 2:54 am

    a destructor should be virtual when at least one of class methods is virtual

    This is a rule of thumb which arises from the fact that when you use virtual functions you are using runtime polymorphism and are more likely to run into situations were you need to destroy a class that may be of a derived type when all you have is a pointer to its base class subobject.

    When you destroy a derived object by using delete on a pointer to the base class a virtual destructor is necessary in the base class to avoid undefined behavior. This is the only time a virtual destructor is necessary and the guideline is intended to help avoid this situation arising.

    Herb Sutter advocated the guideline that base class destructors (i.e. destructors for classes designed to be inherited from) should be either public and virtual or protected and non-virtual. This allows the possibility that you don’t intend a base class to be a point in the inheritance hierarchy which is used for deletion of derived objects and you want to enforce that this doesn’t occur unintentionally.

    Of course, if you have a pure value class which isn’t to be a base class, there is little you can do to stop people deriving from it anyway and then deleting derived class via pointer to base.

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

Sidebar

Related Questions

I have a tricky question. As everyone knows, Apple clearly says that ONE UIViewController
So everyone says that sessions have security risks, I want to know what kind
I have a logical statement that says If everyone plays the game, we will
I suppose this is two very closely related questions: Everyone says to start with
First, I want to let everyone know that I am using an aspx engine
I find a lot of posts where it is explained that one should always
This has always confused me. Everyone says that IE6 dies slowly. But what is
This question is famous but everyone always says go to see this Notepad Tutorial
everyone. This is a piece of code from using sqlite in android tutorial: return
Everyone at Class too big and hard to add new features is completely unphased

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.