我来自 PHP。在 PHP 中,我们可以将文件处理程序返回给一个变量:
class FileHandler
{
private $_fileHandler;
public function __construct()
{
$this->_fileHandler = fopen('log.txt', 'a');
}
public function writeToFile($sentence)
{
fwrite($this->_fileHandler, $sentence);
}
}
我面临的问题是,在 c++ 中,当我希望它分配给成员时它会出错,因此我可以通过我的班级使用它
FileUtils::FileUtils()
{
// I do not what type of variable to create to assign it
string handler = std::ofstream out("readme.txt",std::ios::app); //throws error.
// I need it to be returned to member so I do not have to open the file in every other method
}