0

fstream-pointer をカプセル化するクラスmyclassがあります (fstream にはプライベート代入演算子が宣言されているため、単に fstream ではないため、myclassのインスタンスを正しくコピーできませんが、それは問題ではありません!)。

また、私のクラスには、fstream オブジェクトをそれぞれ開いたり閉じたりするopen()close()という 2 つの関数があります。fstream が開いているという事実は、 myclass 内のプライベート変数bool isOpenに記録されます。

したがって、myclassのインスタンスに実装された他の関数は、 isOpen == true の場合にのみ実行できます。

myclassのインスタンスをコピーするときに、 fstream-pointer が null および/またはisOpen == falseを指すようにしたいと思います。このようにして、異なるインスタンスが同じファイルを使用することを許可しません。ある意味で、私は別のインスタンスが持つ価値を保護しています。

では、*myclass の代入演算子を定義するにはどうすればよいでしょうか? それとも、何か他の方法がありますか?

4

1 に答える 1

1

You must implement a copy constructor AND an assignment operator for your class.

Also (if you still use a pointer member to the fstream object despite "Konrad Rudolf" suggested not to do so) do not forget to delete the pointer in your destructor to have the destructor of the fstream object called that closes the opened file. You cannot be sure that each open() will have a close() pair.

于 2013-01-15T20:54:55.443 に答える