close()
is a member function defined in the class ofstream
. It's not a destructor; it's not special in any way which fundamentally matters to the rules of the C++ language. For that matter, s = ofstream("anotherone.txt")
is actually just another way to spell the line of code s.assignment_operator(ofstream("anotherone.txt"))
, except that instead of assignment_operator
it's spelled operator=
. (Note that in this case, the value which is passed to the member function is a temporary value, and is destroyed after the function returns. That doesn't necessarily imply anything specific about what happens with opening and closing files; it's just how the language works.)
A global variable exists throughout the entire life of the program; the precise semantics during startup and shutdown are a little tricky, but certainly during the "main phase" they always exist. The design of ofstream
may put s
into a different sort of state when s.close()
is called, but that's entirely down to the design of ofstream
, and is something that you'd discover by reading the documentation of that class rather than by reading the rules of the language.