Monday, May 24, 2010

Different destructors in C++?

Is there a reason why there is only one destructor per class in C++? Is there a reason why constructors and destructors aren't paired? C++ automatically creates a default AND a copy-constructor, but there is only one, default destructor. Why isn't it possible to have destructors matching constructors? For example, if I declared a constructor for class X:





X(int, double);





why can't I also declare (and define) a matching destructor, e.g.





~X(int, double);





which would be called only for destructing objects created using the matched constructor above? Please, only serious answers by C++ experts! I know current C++ syntax does not support it, my question is WHY?

Different destructors in C++?
Just suppose, that it's possible. It would means that different objects of the same class have different way to perform the same task (release their resource, clean up , etc). It mean that they have different behavior, which is the antithesis of a class. If that task need to be performed differently based on its construction, maybe some other methods should be done differently too. If object need to behave differently, they need not be instance of the same class. It would also cause an instance and its identical clone (constructed via a copy constructor) to be destroyed differently...





Suppose you have an array of instance of class X, you couldn't call delete[] on it, you would have to give the correct parameters for each X in the array, without even knowing how they were constructed and how many parameters were required for their construction. Would you have the compiler add a secret member to remember which kind of object X each instance is, so that the correct destructor could be virtually called based on the value of this member? Actually what would be the meaning of the parameter received by the destructor? And who would provide this value?





The reality is that once constructed, all instance should provide the same interface. Constructors perform the conversion (something) -%26gt; yourClass, and this may be performed differently based on the something. Destructor on the other hand perform the task (yourClass -%26gt; void) and this require no other input on your part.








Hope this help
Reply:What purpose would it serve? There could be many ways to create an object, but, when you are done with that object, why would you need several ways to destroy it? You either want the object or you don't.
Reply:If there were more than 1 destructor, how would the compiler know which one to call? Constructors and destructors are paired, every class gets one of each, even if you don't implicitly declare a constructor / destructor the compiler automatically generates one to take care of member variables. Destructors are parameterless, this is by compiler design. I think it's bad form to allow a user to force a class to take a parameter when being destroyed. Better to provide a function for that, i.e. myClass.cleanup(myObject); Or, inside the class, if you destructor could branch, make the destructor figure out how to deallocate itself. It's much easier to just say 'delete myClass;' than to try and figure out how it's supposed to be destroyed.


No comments:

Post a Comment