Why we can't overload destructor ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why we can't overload destructor ?

6th Feb 2017, 11:18 AM
Nilesh Tawar
Nilesh Tawar - avatar
2 Answers
+ 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...
6th Feb 2017, 11:27 AM
GeekyShacklebolt
GeekyShacklebolt - avatar
+ 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.
6th Feb 2017, 1:13 PM
~datell