close()我使用时需要手动拨打电话std::ifstream吗?
例如,在代码中:
std::string readContentsOfFile(std::string fileName) {
std::ifstream file(fileName.c_str());
if (file.good()) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
throw std::runtime_exception("file not found");
}
Run Code Online (Sandbox Code Playgroud)
我需要file.close()手动拨打电话吗?不应该ifstream使用RAII来关闭文件?
假设我有以下代码:
struct mytype
{
~mytype() { /* do something like call Mix_CloseAudio etc */ }
};
int main()
{
mytype instant;
init_stuff();
start();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使从start()内部的某个地方使用exit(),是否可以保证调用析构函数?