+ 6

Why destructor can not be more than one in c++?

only one destructor is applicable in one class as compared to constructor we can add as many with the help of overloading I just want to understand it!! can't we define the ending of the program in our own way with some conditions on to it. like we do in python with exception syntax!!

14th Apr 2017, 11:31 AM
Vishesh Saxena
Vishesh Saxena - avatar
20 ответов
+ 16
Though, from first look anyone might think that we can add arguements to destructor to make it vary. But the thing is, there is no statement to execute it. A destructor automatically initiates. So, you can not make more destructors than 1. Hope you got it buddy.
14th Apr 2017, 11:44 AM
DEVIL kingg
DEVIL kingg - avatar
+ 10
It isnt possible to accept arguments in a destructor. So it cannot be overloaded. Multiple destructors with same name will result in error.
14th Apr 2017, 11:32 AM
Pixie
Pixie - avatar
+ 9
By asking this question, you're missing the point of destructors. They exist so you don't have to worry about manually freeing your object's dynamic memory when it falls out of scope. Because they get called automatically, it wouldn't make sense to allow parameters-- How would the compiler decide what arguments to use when calling it? If you want to have multiple "destructors" with different parameters, all you need to do is write them as member functions. You can still have your default destructor that gets called automatically in addition to whatever special disposal procedures you want to add and call manually. PS: Contrary to the top answer right now, it is possible to call the destructor manually: "object.~Object();" is perfectly valid.
16th Apr 2017, 7:03 PM
Squidy
Squidy - avatar
+ 7
because it aint Jesus and it cant be killed more than once
28th Apr 2017, 1:07 AM
DeleteThisAccount
+ 6
kamran, virtual destructors are absolutely deterministic and, though non trivial are a perfect counterbalance to the question. As such they really need an explanation that I shan't attempt to do justice to.
16th Apr 2017, 8:55 AM
Leon
Leon - avatar
+ 3
multiple destructors come in through inheritance, and are called deterministicly in c++
15th Apr 2017, 1:26 PM
Kamran Tavakoli
Kamran Tavakoli - avatar
+ 3
Because you can construct a class one time while, classes can only be destroyed once, therefore 1 destructor is enough
21st Apr 2017, 7:41 AM
MrCoder
MrCoder - avatar
+ 2
because the constructor can be many depending upon the number of arguments(parameters) but destructor is unparametersed in nature so it's enough to use once .the ultimate goal is to free the space that occupy by the constructor...and it achieve using that once
15th Apr 2017, 10:44 PM
Chaman Raghav
Chaman Raghav - avatar
+ 1
I have not tried..but i think it is useless for any class to use more then one destructor while you can uninitialized or free the variable by single one
15th Apr 2017, 5:51 PM
Devashish gupta
Devashish gupta - avatar
+ 1
The answer is easy and straightforward.If you need to construct some object you can do it by several ways(several constructors).Resources allocation and using memory of the object be changing through the life cycle of the object but destroying of the object has only one state and it is to free all allocated resources and used memory.The destroyer must make sure this state.From that reasons other destructors are wasted.If you need to keep some other info helps you to do it you can use member variables for it.
17th Apr 2017, 8:37 PM
Highman
Highman - avatar
+ 1
because there is no need, its automatically called when object comes out of scope,that is its work is done ,there is no need of that so free memory it is used.
23rd Apr 2017, 5:10 AM
Kuldeep Parashar
Kuldeep Parashar - avatar
+ 1
why do you need various destructors? when you destroy something, don't need to destroy it anymore.
26th Apr 2017, 4:53 AM
Swidon Mola
Swidon Mola - avatar
+ 1
theres only one right way to delete your class object, and you need to have it there.
27th Apr 2017, 9:17 PM
Mark Sopel
Mark Sopel - avatar
+ 1
only one destructor because destructor cannot be overloaded. So only one to deallocate memory.
30th Apr 2017, 9:22 AM
Mexson Fernandes
Mexson Fernandes - avatar
0
You only do overloading because of the parameters you wanna pass into the function. Why would you wanna try overloading any function that doesn't receive arguments? Besides, Overloading allows a function to perform tasks based the arguments... Destructors have a sole purpose which is mainly to release resources and stuffs like that.
23rd Apr 2017, 9:56 PM
Stark
Stark - avatar
0
when class object initialize constructor construct on ram when you need to destroy on ram automaticaly calls destructor which func destroy initialized object of course this makes you can call them one time per object
24th Apr 2017, 5:38 PM
Burak Usluer
Burak Usluer - avatar
0
one destructor can release memory from all data members of It's class in which it defined
3rd May 2017, 2:48 PM
Gulfam Haider
Gulfam Haider - avatar
0
answer is we cannot overload and a destructor can't have any parameters.
13th Jan 2020, 11:30 AM
Imanuel Wicaksono
Imanuel Wicaksono - avatar
0
Multiple answer A destructor can't have any parameters We cannot over load a destructir
28th May 2020, 2:29 AM
Muvvala Sri Vishnu Deepak
Muvvala Sri Vishnu Deepak - avatar
- 1
destructor is automatically invoked when an object is destroyed so we cannot have more than one destructor
19th Apr 2017, 5:53 PM
Rajini Mandula
Rajini Mandula - avatar