If std::fstream's destructor calls <close()> itself, why is <close()> not a private function?
For scenarios where the user wants to close the file before the destruction. The same reason why
unique_ptr::reset() or
vector::clear() exist, and you hopefully don't call them at end of scope, either...
You should read my article in the first post. If something in C++ forces you to explicitly close or destroy the object, it's badly designed.
But then how does that work? Does the variable disappear from memory when the function ends?
The object is destroyed at the end of scope. Variables are just a way to access objects in your code, they don't exist at runtime. Scopes are absolutely crucial in C++, maybe you should read up on that topic...
What do you mean by "ugly" code?
I mean code that is guided by myths such as "declarations are very inefficient, so I only declare the variable once but with a much wider scope". The priority should be code clarity and correctness, not questionable micro-optimizations.