Overloading Destructors !

Discussion in 'C++' started by Bhullarz, Feb 21, 2008.

  1. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    is it possible to overload Destructors in C++ ? If yes, what is the need and how to ?
     
  2. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    Not at all possible...

    but this is very intresting thing..

    Let seee any one comment
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I disagree.

    You can overload destructor's because you need to make them virtual as well so the destructor's are called.

    A difference between a destructor 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.
     
  4. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    It would be very nice if you support your answer with some example...

    Thanks
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Try Running the program
    Code:
    #include <iostream.h>
    class Base
    {
    public:
    	Base(){ cout<<"Constructor: Base"<<endl;}
    	~Base(){ cout<<"Destructor : Base"<<endl;}
    };
    
    class Derived: public Base
    {
    public:
    	Derived(){ cout<<"Constructor: Derived"<<endl;}
    	~Derived(){ cout<<"Destructor : Derived"<<endl;}
    };
    void main()
    {
    	Base *Var = new Derived();
    	delete Var;
    }
    and then add the virtual to the base class and run and see what is the difference. Which means when you override the class constructor and destructor and by default inherited and overloaded.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I meant virtual to the base class destructor
     
  7. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    Bro ! I am asking about Destructor Overloading same as we do with the Constructor.. Like Multiple Constructors in a class with different argument. But your example is about Virtual Destructor..
    If I'm wrong, rectify me.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What I have given as a code is also for overloading destructor but it looks like what you are looking for is whether we can define more than one destructor in a class or not ?

    The answer is no for couple of reason.

    1. Syntax wise we cannot have them as you cannot have params in the destructor
    2. Semantically you don't need to clean up the object in more than one ways.
     
  9. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    Shabbir ! According to your code, we are defining the destructor as virtual but what is the difference in defining a normal function as virtual and destructor as virtual. Actually we define a virtual destructor so that complete memory can be released or because of security reasons like avoiding memory leaks.... I wasn't aware it is called Destructor Overloading....
     
  10. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    shabbir, the example that you have posted is not of destructor overloading...

    and even in the case when u redefine a member function of the base class in the derived class, it is called function overriding..
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    :iagree:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice