+ 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!!
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.
+ 10
It isnt possible to accept arguments in a destructor. So it cannot be overloaded. Multiple destructors with same name will result in error.
+ 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.
+ 7
because it aint Jesus and it cant be killed more than once
+ 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.
+ 3
multiple destructors come in through inheritance, and are called deterministicly in c++
+ 3
Because you can construct a class one time while, classes can only be destroyed once, therefore 1 destructor is enough
+ 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
+ 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
+ 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.
+ 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.
+ 1
why do you need various destructors? when you destroy something, don't need to destroy it anymore.
+ 1
theres only one right way to delete your class object, and you need to have it there.
+ 1
only one destructor because destructor cannot be overloaded. So only one to deallocate memory.
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.
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
0
one destructor can release memory from all data members of It's class in which it defined
0
answer is we cannot overload and a destructor can't have any parameters.
0
Multiple answer
A destructor can't have any parameters
We cannot over load a destructir
- 1
destructor is automatically invoked when an object is destroyed so we cannot have more than one destructor