Reputation: 1663
I have a class myclass that encapsulate an fstream-pointer (not simply fstream because of the fact that fstream have a private assignment operator declared, so I can't correctly copy an instance of myclass, but that's not the problem!).
Also, my class has two functions, open() and close() which opens and closes the fstream object respectively. The fact that the fstream is open, is recorded into a private variable bool isOpen inside myclass.
So, if any other function implemented into the instance of myclass can be executed only if isOpen == true.
I would like that, when I copy an instance of myclass, the fstream-pointer point to null and/or isOpen == false in every case. In this way, I don't allow different instances to use the same file. In a certain way, I'm protecting the value that another instance has.
So, how can I define the assignment operator of *myclass? Or maybe, there is some other way?
Upvotes: 0
Views: 91
Reputation: 5855
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.
Upvotes: 1