468,512 Members | 1,341 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Is it possible to Overload a Destructor?

12
Hi Banfa ,
Can we overload destructor ? If yes then plz provide an example ?
If not then why ?
Aug 30 '06 #1
8 10890
Hi Banfa ,
Can we overload destructor ? If yes then plz provide an example ?
If not then why ?

Answere

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. class abc
  5. {
  6. public:
  7. abc()
  8. {
  9. printf("Constructor is called\n");
  10. }
  11. ~abc()
  12. {
  13. printf("Destructor is called\n");
  14. }
  15. };
  16. class xyz:abc
  17. {
  18. public:
  19. xyz()
  20. {
  21. printf(" Derived Constructor is called\n");
  22. }
  23. ~xyz()
  24. {
  25. printf(" Derived Destructor is called\n");
  26. }
  27. };
  28. void main()
  29. {
  30.  
  31. clrscr();
  32. abc *a = new abc();
  33. xyz *x = new xyz();
  34. //abc *b = new xyz();
  35. delete a;
  36. delete x;
  37. //delete b;
  38. getch();
  39. }
  40.  
:)
Aug 30 '06 #2
anupam
12
What was that dhirajrane ? I didn't mean the general constructor and destructor. It was overloaded destructor ( whether we can define more than one destructor in a class or not ) ?
Aug 30 '06 #3
D_C
293 100+
Why would you need to delete something in more than one way? I don't see why it's necessary. Besides, you could always call a single destructor, and conditionally execute whichever destructor you wish.
Aug 30 '06 #4
Banfa
9,056 Expert Mod 8TB
Normally (in fact almost always) you never explicitly call the destructor, it is automatically called by the C++ frame work when the object is deleted/goes out of scope. Therefore you would not need to overload the destructor because the frame work always calls the default destructor.

Additionally the C++ standard defines that a conforming compiler only allows for 1 destructor per class.
Aug 30 '06 #5
anupam
12
Thanks all of u.
Aug 31 '06 #6
There is no reason for overloading (i mean two function with same name) a destructor in c++, because a destructor is called automatically at last and it will clean the allocated memory. so once allocated memory is de-allocated by one destructor what the need for another function, so only c++ doesn't support destructor overloading .
Jan 1 '13 #7
sripal
1
Uday


i have one Question here,
i got one point we cant overload a destructor,
can we have override destuctor?
Nov 27 '14 #8
Banfa
9,056 Expert Mod 8TB
No, destructors (and constructors) are special and you can not over-ride a destructor, nor should you need to.

In a class heirarchy

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     Base() {}
  5.     ~Base() {}
  6. };
  7.  
  8. class Derived : public Base
  9. {
  10. public:
  11.     Derived() {}
  12.     ~Derived() {}
  13. };
  14.  
When you construct an instance of Derived after the memory is allocated the first thing that happens is the Base() is called, the Derived() is called.

Destruction happens in a similar manor but reverses the order, first ~Derived() is called, then ~Base() is called.

You can not over-ride ~Base in Derived because the compiler knows that the method starting with ~ is the destructor and that the symbol name following should be the name of the class. There is also no need to over-ride as the Base destructor is going to be called anyway and should be doing the appropriate clean-up for Base.
Nov 28 '14 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Nimmi Srivastav | last post: by
11 posts views Thread by Ken Durden | last post: by
35 posts views Thread by Peter Oliphant | last post: by
23 posts views Thread by Ben Voigt | last post: by
9 posts views Thread by rohits123 | last post: by
3 posts views Thread by Juha Nieminen | last post: by
12 posts views Thread by Rahul | last post: by
1 post views Thread by fabian.lim | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.