Thread: derived destructor problem in inheritance

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    derived destructor problem in inheritance

    Hi;
    I have a short question about pointer and derived inheritance
    I have a class Base and a class Derived; they have constructor and destructor.

    in main I create it
    Base *p=new Derived();
    but the output is
    Base Constructor
    Derived Constructor
    Base Destructor
    .......................... where is Derived Destructor ? why It is not called ?
    Code:
    #include<iostream>
    
    using namespace std;
    
    class Base
    {
    public:
        Base()  {cout<<"Base Constructor"<<endl;}
        ~Base() {cout<<"Base Destructor"<<endl;}
    };
    
    class Derived: public Base
    {
        public:
        Derived() {cout<<"Derived Constructor"<<endl;}
        ~Derived() {cout<<"Derived Destructor"<<endl;}
    };
    
    int main()
    {
        Base *p=new Derived();
        delete p;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    watch this:
    Buckys C++ Programming Tutorials - 52 - Inheritance - YouTube

    This breaks down this question for you.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In short: your code has undefined behaviour.

    Base's destructor needs to be declared virtual.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    thank you I got it =)
    if without virtual destructor
    Code:
    #include<iostream>
    
    using namespace std;
    
    class Base
    {
    public:
        Base()  {cout<<"Base Constructor"<<endl;}
        ~Base() {cout<<"Base Destructor"<<endl;}
    };
    
    class Derived: public Base
    {
        public:
        Derived() {cout<<"Derived Constructor"<<endl;}
        ~Derived() {cout<<"Derived Destructor"<<endl;}
    };
    
    int main()
    {
        Base *p=new Derived();
        delete p;
    
        return 0;
    }
    


    OUTPUT
    Base Constructor
    Derived Constructor
    Base Destructor


    if with virtual destructor
    Code:
    #include<iostream>
    
    using namespace std;
    
    class Base
    {
    public:
        Base()  {cout<<"Base Constructor"<<endl;}
       virtual ~Base() {cout<<"Base Destructor"<<endl;}
    };
    
    class Derived: public Base
    {
        public:
        Derived() {cout<<"Derived Constructor"<<endl;}
        ~Derived() {cout<<"Derived Destructor"<<endl;}
    };
    
    int main()
    {
        Base *p=new Derived();
        delete p;
    
        return 0;
    }
    




    OUTPUT
    Base Constructor
    Derived Constructor
    Derived Destructor
    Base Destructor

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cena06 View Post
    thank you I got it =)
    No you haven't.

    If Base does not have a virtual destructor, the behaviour of your code is undefined. The output you describe is only one (albeit a common one in practice) of an infinite number of possibilities.

    If Base has a virtual destructor, the behaviour is defined, and will reliably produce the output you describe.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor Problem
    By almawajdeh in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2010, 02:42 PM
  2. Destructor Problem
    By ltanusaputra in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2007, 12:05 PM
  3. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 04:31 PM
  4. Destructor not called in inheritance
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2005, 05:21 PM
  5. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM