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?

12
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 ?

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 =...
1
isladogs
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...
0
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...
0
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...
0
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...
0
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...
0
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...
0
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...
1
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...
0
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.