473,215 Members | 1,290 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,215 software developers and data experts.

Is it possible to Overload a Destructor?

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


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  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. {
  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. }
Aug 30 '06 #2
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
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
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
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

i have one Question here,
i got one point we cant overload a destructor,
can we have override destuctor?
Nov 27 '14 #8
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. };
  8. class Derived : public Base
  9. {
  10. public:
  11.     Derived() {}
  12.     ~Derived() {}
  13. };
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

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...
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...
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...
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...
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....
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);...
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...
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? ...
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 =...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.