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

this example below works when hover event is trigered and when its not, its
Given the example below, can someone please show me how this could be called?
In the Jquery example below, I would like to expand $(this) before it is
For some reason I'm not getting this. (Example model below) If I write: var
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
I know how to do this... I'll give example code below. But I can't
This example is from php.net: <?php function Test() { static $a = 0; echo
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
I need to copy-construct an object simultaneously changing it's type to another class being
The example below illustrates a more complex but not dissimilar problem I've been trying

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.