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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:19:02+00:00 2026-05-27T20:19:02+00:00

As I know, any class that is designated to have subclasses should be declared

  • 0

As I know, any class that is designated to have subclasses should be declared with virtual destructor, so class instances can be destroyed properly when accessing them through pointers.

But why it’s even possible to declare such class with non-virtual destructor? I believe compiler can decide when to use virtual destructors. So, is it a C++ design oversight, or am I missing something?

  • 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-27T20:19:03+00:00Added an answer on May 27, 2026 at 8:19 pm

    Are there any specific reasons to use non-virtual destructors?

    Yes, there are.

    Mainly, it boils down to performance. A virtual function cannot be inlined, instead you must first determined the correct function to invoke (which requires runtime information) and then invoke that function.

    In performance sensitive code, the difference between no code and a “simple” function call can make a difference. Unlike many languages C++ does not assume that this difference is trivial.

    But why it’s even possible to declare such class with non-virtual destructor?

    Because it is hard to know (for the compiler) if the class requires a virtual destructor or not.

    A virtual destructor is required when:

    • you invoke delete on a pointer
    • to a derived object via a base class

    When the compiler sees the class definition:

    • it cannot know that you intend to derive from this class — you can after all derive from classes without virtual methods
    • but even more daunting: it cannot know that you intend to invoke delete on this class

    Many people assume that polymorphism requires newing the instance, which is just sheer lack of imagination:

    class Base { public: virtual void foo() const = 0; protected: ~Base() {} };
    
    class Derived: public Base {
      public: virtual void foo() const { std::cout << "Hello, World!\n"; }
    };
    
    void print(Base const& b) { b.foo(); }
    
    int main() {
      Derived d;
      print(d);
    }
    

    In this case, there is no need to pay for a virtual destructor because there is no polymorphism involved at the destruction time.

    In the end, it is a matter of philosophy. Where practical, C++ opts for performance and minimal service by default (the main exception being RTTI).


    With regards to warning. There are two warnings that can be leveraged to spot the issue:

    • -Wnon-virtual-dtor (gcc, Clang): warns whenever a class with virtual function does not declare a virtual destructor, unless the destructor in the base class is made protected. It is a pessimistic warning, but at least you do not miss anything.

    • -Wdelete-non-virtual-dtor (Clang, ported to gcc too): warns whenever delete is invoked on a pointer to a class that has virtual functions but no virtual destructor, unless the class is marked final. It has a 0% false positive rate, but warns “late” (and possibly several times).

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

Sidebar

Related Questions

I'd like to know if there is any .Net class that allows me to
Does anyone know any good tool that I can use to perform stress tests
Does .NET 4 come with any class that serializes/deserializes JSON data? I know there
How do I know which initializer is the designated initializer for ANY class? I'm
I'd like to know if there is any class in Java able to check,
I'd like to know if there's any way to force the System.Net.Dns class to
Do you know any libraries similar to java.util.Properties that support more advanced features like
Say I have below lines any in a Java class, System.out.println(start); if(true)//The compiler will
First, I understand what this means: DockLayoutPanel does not extend any class that in
I have a LinkedIn API class that I've written for PHP that makes use

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.