NO
This is what RAII is for, let the destructor do its job. There is no harm in closing it manually, but it's not the C++ way, it's programming in C with classes.
If you want to close the file before the end of a function you can always use a nested scope.
In the standard (27.8.1.5 Class template basic_ifstream), ifstream
is to be implemented with a basic_filebuf
member holding the actual file handle. It is held as a member so that when an ifstream object destructs, it also calls the destructor on basic_filebuf
. And from the standard (27.8.1.2), that destructor closes the file:
virtual ?basic_filebuf();
Effects: Destroys an object of class basic_filebuf<charT,traits>
. Calls close()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…