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

  • Home
  • SEARCH
  • 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 9069751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:30:42+00:00 2026-06-16T17:30:42+00:00

using namespace std; class C { int a; public: C(int aa=0) {a=aa;} ~C() {cout

  • 0
using namespace std;

class C
{
    int a;
    public:
        C(int aa=0) {a=aa;}
        ~C() {cout << "Destructor C!" << a << endl;}
};

class D: public C
{
    int b;
    public:
        D(int aa=0, int bb=0): C(aa) {b=bb;}
        ~D() {cout << "Destructor D!" << b << endl;}
};

void test()
{
    D x(5);
    D y(6,7);
}

int main()
{
    test();
}

Above is the code, and below is the running result:

Destructor D!7
Destructor C!6
Destructor D!0
Destructor C!5

I don’t understand why “Destructor C!” will be invoked. And the related destructor’s calling sequence. I feel that it seems like the stack push/pop.

Further:
Why it calls “D x(5);” earlier but the corresponding result is given later?

  • 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-06-16T17:30:43+00:00Added an answer on June 16, 2026 at 5:30 pm

    The destructor calling sequence always goes from the derived to base, like popping the stack. This allows derived classes to clean up resources allocated by your base class. In this case, the compiler knows how to construct this sequence, because it knows the exact type of the objects x and y statically.

    There are situations, however, when this sequence would be broken. Consider the following modification of your code:

    void test()
    {
        C *x = new D(5);
        D *y = new D(6,7);
        delete x;
        delete y;
    }
    

    It produces the following output:

    Destructor C!5
    Destructor D!7
    Destructor C!6
    

    Running this produces no call of ~D for x; for y, both destructors are called.

    This is because you did not declare the destructor virtual in the base class. When the destructor is not virtual, the compiler has no idea that it must call the destructor of the derived class in situations when an object is referred to by a pointer to a base class. This is why you should always make a destructor virtual in classes that must be inherited and allocate resources dynamically.

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

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
Code : #include<iostream> using namespace std; class A{ public: A(){cout << A() Constructor <<
#include<iostream> using namespace std; class test { int a; public: test() { a=0; }
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
#include <iostream> using namespace std; class Ex { private: int i; float f; public:
Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()

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.