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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:53:40+00:00 2026-05-16T18:53:40+00:00

I didn’t see the answer to this in the C++ Faq lite: How do

  • 0

I didn’t see the answer to this in the C++ Faq lite:

How do I define a base class so every class inheriting it is required to define a destructor?

I tried running this program

struct VDtor { virtual ~VDtor()=0;  };
struct Test:VDtor { virtual ~Test(){}  };
int main() { delete new Test; return 0; }

http://codepad.org/wFcE71w3
With the error

In function `Test::~Test()':
t.cpp:(.gnu.linkonce.t._ZN4TestD0Ev+0x1e): undefined reference to `VDtor::~VDtor()'
In function `Test::~Test()':
t.cpp:(.gnu.linkonce.t._ZN4TestD1Ev+0x1e): undefined reference to `VDtor::~VDtor()'

So, is it possible?

  • 1 1 Answer
  • 1 View
  • 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-16T18:53:41+00:00Added an answer on May 16, 2026 at 6:53 pm

    It is “possible” in some sense (if your goal was that the derived class stays abstract otherwise). But it won’t give the result you would like: Because the compiler will create a destructor itself implicitly if the programmer hasn’t done so.

    It’s therefor not possible to force the derived class’ author to explicitly declare a constructor.

    (edit: Like @chubsdad notes noted, the error in your particular code is because you need to define the explicitly declared destructor of the base class).


    Edit: Just for fun, there are situations that necessiate an explicitly declared constructor. Consider the following

    struct Viral {
      struct Dose { };
    protected:
      ~Viral() throw (Dose) { }
    };
    
    struct Base {
      virtual ~Base() throw() { }
    };
    
    struct Derived : Base, Viral { };
    

    This code won’t compile because the implicitly declared ~Derived will have an exception specification throw (Dose) which is looser than what ~Base has – so it violates the requirement that overriders shall not have a looser exception specification. You will need to explicitly declare the destructor appropriately

    struct Derived : Base, Viral { ~Derived() throw() { } };
    

    But this is not really a solution to your problem, because derived classes need to “cooperate” into either deriving from Viral or putting it as a non-static data member. It’s also very ugly 🙂


    Edit: The following seems to be a Standard conforming way to do it

    struct Viral {
      struct Dose { };
    protected:
      ~Viral() throw (Dose) { }
    };
    
    struct Base : virtual Viral {
      virtual ~Base() throw() { }
    };
    

    Clang and GCC (starting with v4.6) reject any derived class of Base that has an implicitly declared destructor, because it has an incompatible exception specification (any derived class shall call ~Viral directly, instead of indirectly by calling ~Base, the Standard says). Comeau accepts this, but I strongly suspect that it is non-conforming in this regard.

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

Sidebar

Related Questions

I didn't get the answer to this anywhere. What is the runtime complexity of
I didn't see any similar questions asked on this topic, and I had to
I didn't want to ask this as an 'answer' to A Question Someone Beat
I didn't see a clear answer from the same question How can I figure
Didn't see a question similar to this, so here goes: Let's say I'm running
didn't find an answer to this. I like KeyedCollection, because it keeps the insertion
I didn't see any questions that asked this previously so here goes: Once you
Didn't got any luck finding an answer on google , so this is my
Didn't find any from their website. I mostly just trying to see which one
I didn't see the option to point the workspace (or it's VS equivalent, I'm

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.