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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:49:13+00:00 2026-05-24T15:49:13+00:00

This example below illustrates how to prevent derived class from being copied. It’s based

  • 0

This example below illustrates how to prevent derived class from being copied. It’s based on a base class where both the copy constructor and copy assignment operator are declared private.

class Uncopyable
{
protected:
   // allow construction and destruction of derived objects...
   Uncopyable() {}
   ~Uncopyable() {}

private:
   // but prevent copying...
   Uncopyable(const Uncopyable&);
   Uncopyable& operator=(const Uncopyable&);
};

We can use this class, combined with private inheritance, to make classes uncopyable:

class derived: private Uncopyable
{...};

Notice that the destructor in class Uncopyable is not declared as virtual.

Previously, I learned that

  • Destructors in base classes should be virtual.
  • Destructors in non-base classes should not be made virtual.

In this example, the destructor for Uncopyable is not virtual, but it is being inherited from. This seems to go against the wisdom I’ve learned previously.

When and why should destructor in base class NOT be defined as virtual?

  • 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-24T15:49:16+00:00Added an answer on May 24, 2026 at 3:49 pm

    A base class destructor only needs to be virtual if you might try deallocating an object of a derived type through a pointer of the base type. Consequently, if you only inherit from the base class privately instead of publicly, as would be the case in Uncopyable, then you don’t need to worry about putting in a virtual destructor, because when using private inheritance you can’t get a pointer to the derived object and store it in a pointer to the base type.

    Another example might be if you were to use a mixin class like this one that makes a class track the number of object allocations, where the mixin is inherited from to acquire behavior but not to be treated polymorphically:

    template <typename T> class Counter {
    public:
        Counter() { ++numInstances; }
        Counter(const Counter&) { ++numInstances );
        ~Counter() { --numInstances; }
    
        static unsigned getNumInstances() { return numInstances; }
    
    private:
        static unsigned numInstances;
    }
    template <typename T> unsigned Counter<T>::numInstances = 0;
    

    More generally, when using static polymorphism, you don’t need virtual destructors because you never treat the classes polymorphically using pointers to the base type. You only use a pointer to the derived type.

    There are probably a few other cases I didn’t cover here, but these two (private inheritance, mixin classes, and static polymorphism) cover much of the space where virtual destructors aren’t required.

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

Sidebar

Related Questions

For some reason I'm not getting this. (Example model below) If I write: var
This example is from php.net: <?php function Test() { static $a = 0; echo
Given this example: <img class=a /> <img /> <img class=a /> <img class=a id=active
Consider this example (typical in OOP books): I have an Animal class, where each
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
This is beyond both making sense and my control. That being said here is
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Following this example, I can list all elements into a pdf file import pyPdf
In this example the .Stub returns a new memory stream. The same memory stream

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.