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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:52:00+00:00 2026-05-15T17:52:00+00:00

A difference between a destructor (of course also the constructor) and other member functions

  • 0

A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed?

It will be great to know what exactly happens in case of destructor (maybe virtual) & constructor, that they are called for all its base classes even if the most derived class object is deleted.

Thanks in advance!

  • 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-15T17:52:01+00:00Added an answer on May 15, 2026 at 5:52 pm

    The Standard says

    After executing the body of the destructor and destroying any automatic objects allocated within the body,
    a destructor for class X calls the destructors for X’s direct non-variant members,the destructors for X’s direct
    base classes
    and, if X is the type of the most derived class (12.6.2), its destructor calls the destructors for
    X’s virtual base classes
    . All destructors are called as if they were referenced with a qualified name, that is,
    ignoring any possible virtual overriding destructors in more derived classes. Bases and members are destroyed
    in the reverse order of the completion of their constructor
    (see 12.6.2). A return statement (6.6.3) in a
    destructor might not directly return to the caller; before transferring control to the caller, the destructors
    for the members and bases are called. Destructors for elements of an array are called in reverse order of
    their construction (see 12.6).

    Also as per RAII resources need to be tied to the lifespan of suitable objects and the destructors of respective classes must be called upon to release the resources.

    For example the following code leaks memory.

     struct Base
     {
           int *p;
            Base():p(new int){}
           ~Base(){ delete p; } //has to be virtual
     };
    
     struct Derived :Base
     {
           int *d;
           Derived():Base(),d(new int){}
           ~Derived(){delete d;}
     };
    
     int main()
     {
         Base *base=new Derived();
         //do something
    
         delete base;   //Oops!! ~Base() gets called(=>Memory Leak).
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Please Note: This question is about the difference in terminology between the words destructor
Is there a difference between TerminateExtension() and the extensions destructor? Obviously both are used
Is there any difference between a protected and a private destructor in C++? If
What is really the difference between the algorithms remove and remove_if and the member
Difference between Java & .Net Framework Regular Expressions Pattern I am trying to convert
What is the difference between window.onload , document.onready and body.onload ?
What is the difference between early and late binding?
Is there a performance difference between i++ and ++i if the resulting value is
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass

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.