logoalt Hacker News

Panzerschrektoday at 6:58 AM1 replyview on HN

defer is not a proper replacement for destructors. One need to write it manually each time in each function where some cleanup is needed. It's easy to forget to do so or to do this in a wrong way. Destructors in the other hand are called automatically and all cleanup logic is written exactly once (within destructor body).


Replies

cv5005today at 12:55 PM

>One need to write it manually each time in each function where some cleanup is needed.

You can structure your code to not need cleanup in every function.

The biggest problem with destructors is, how do you handle errors? close() can fail you know?

show 1 reply