0
Why we can't overload destructor ?
2 odpowiedzi
+ 7
Why can't we overload a destructor, is simply because it need not to be.
practically there is no advantage of overloading a destructor.
Because destructors are use to deallocate the memory possessed by an object, so for what else purpose do we need to overload it...
+ 1
The reason is that the destructor is called by the C++ runtime and not only manually. Everytime an object that got not allocated using 'new' gets out of scope, its destructor is called. So how should the C++ runtime know which destructor should be called? Maybe a possible solution would be to allow overloading if and only if a default destructor without any arguments is defined.
You see thing would get complicated so the standard decided to make the destructor unique with no arguments.
By the way, most programmers would say it's a bad habbit to call a destructor manually. You should let the runtime do its work and let it call the destructor.