ファイルハンドルをメンバ変数にする

C++


#include

class A_class
{
protected:
std::ofstream file2;

public:
A_class()
{
std::ofstream file1("file1.txt");
file1 << "start file1\n" ;

file2.open("file2.txt");
file2 << "start file2\n" ;
}
~A_class()
{
file2.close();
}

void sayhello()
{
// file1 << "Hello file1\n" ;//<- エラーになる
file2 << "Hello file2\n" ;
}
};

int main()
{
class A_class A_instance ;

A_instance.sayhello();

return 0 ;
}