Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Overloading Destructors ! (http://www.go4expert.com/forums/overloading-destructors-t8967/)

Bhullarz 21Feb2008 13:16

Overloading Destructors !
 
is it possible to overload Destructors in C++ ? If yes, what is the need and how to ?

aisha.ansari84 21Feb2008 14:13

Re: Overloading Destructors !
 
Not at all possible...

but this is very intresting thing..

Let seee any one comment

shabbir 21Feb2008 18:49

Re: Overloading Destructors !
 
Quote:

Originally Posted by aisha.ansari84
Not at all possible...

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.

Bhullarz 21Feb2008 20:30

Re: Overloading Destructors !
 
Quote:

Originally Posted by shabbir
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.

It would be very nice if you support your answer with some example...

Thanks

shabbir 21Feb2008 21:54

Re: Overloading Destructors !
 
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.

shabbir 21Feb2008 21:55

Re: Overloading Destructors !
 
I meant virtual to the base class destructor

Bhullarz 23Feb2008 05:49

Re: Overloading Destructors !
 
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.

shabbir 23Feb2008 09:33

Re: Overloading Destructors !
 
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.

Bhullarz 23Feb2008 10:46

Re: Overloading Destructors !
 
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....

technosavvy 25Feb2008 10:29

Re: Overloading Destructors !
 
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..


All times are GMT +5.5. The time now is 21:30.