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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:41:32+00:00 2026-06-13T06:41:32+00:00

I read that that virtual destructors must be declared in classes that have virtual

  • 0

I read that that virtual destructors must be declared in classes that have virtual methods. I just cant understand why they must be declared virtual. I know why we need to have virtual destructors as from the following example. I just wanted to know why compilers dont manage virtual destructors for us. Is there something I need to know about working of virtual destructors ?
The following example shows that if destructors are not declared virtual the destructors of derived class are not called why is that ?

class Base 
{
    // some virtual methods
public:
    Base()
    {std::cout << "Base Constructor\n";}
    ~Base()
    {std::cout << "Base De-structor\n";}

};

class Derived : public Base
{
public:
    Derived()
    {std::cout << "Der constructor\n";}
    ~Derived()
    { std::cout << "Der De-structor\n";}
} ;         
void main()
{

    Base *b = new Derived();
    delete b;
}
  • 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-13T06:41:34+00:00Added an answer on June 13, 2026 at 6:41 am

    I just wanted to know why compilers dont manage virtual destructors for us.

    Because in C++, you pay for what you use. Having a virtual destructor by default involves the compiler adding a virtual table pointer to the class, which increases its size. This is not always desirable.

    The following example shows that if destructors are not declared virtual the destructors of derived class are not called why is that ?

    The example exibits undefined behavior. It’s simply against the rules. The fact that not all destructors are called is just one possible manifestation. It could possibly crash.

    Is there something I need to know about working of virtual destructors ?

    Yes. They are required if you’re deleting an object through a pointer to a base class. Otherwise it’s undefined behavior.

    5.3.5 Delete [expr.delete]

    3) In the first alternative (delete object), if the static type of the object to be deleted is different from its
    dynamic type
    , the static type shall be a base class of the dynamic type of the object to be deleted and the
    static type shall have a virtual destructor or the behavior is undefined
    . In the second alternative (delete
    array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined. (emphasis mine)

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

Sidebar

Related Questions

I have a Base class, that is considered read-only, and in it's virtual desructor
I just read that we should not use virtual function excessively. People felt that
I have read some vague statement that virtual inheritance doesn't provide the memory structure
From what I've read, virtual functions are functions in the base class that you
I read that Windows wp8 allows native code. So how then have ports such
I have a client that I'm wanting to move to a virtual private server.
i've always read that the user you set in the IIS website/virtual directory is
Possible Duplicate: C++ virtual function table memory cost I've juste read that : http://www.parashift.com/c++-faq-lite/virtual-functions.html
I have read some tips that multithread implementation largely depends on the target OS
I read in a book that CreateInstance is a MUST for a source filter

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.