|
|
|
|
|
by codeflo
1482 days ago
|
|
BTW, a lot of people in this thread compare destructors to Java's try-with-resource (similarly, C#'s using), or Go's defer. Here's one important difference: Destructors work in standard data structures. If for whatever reason you want to build a map<string, list<fstream>>, and the map goes out of scope, all files are correctly closed. (Rust's drop semantics work the same way.) That's a lot more work in Java or C#. |
|
Yes, that means they hang around a lot longer, and that's sometimes problematic, but that's the GC way.