473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 11356
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,065 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,065 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

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

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
3
by: Juha Nieminen | last post by:
This is a simplified version of the situation (the actual situation is quite more complex, but when stripped to the bare minimum, it's like this): class BaseClass { public: virtual void...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.