Having a class without data is a good option for file manipulation?

I have a file with saved data that sometimes needs to be received, written, erased, etc., when the program is running. I decided to write a SavedDataHandler class for this. I am currently using a class fstream.

I believed that one data element is the stream itself, opening it in the constructor and closing it in the destructor. However, I realized that the different functions called by SavedDataHandler open the stream in different ways (setting different flags, etc.), so I decided not to go along this route.

Instead, I only static const std::stringhave the file name, and the public member functions handle opening and closing the file as needed. Performance is not a problem.

Is this route valid? Since it does not have data elements, providing a constructor is not even required. It is just a class that contains functions (and one static constant), with functions running on the resource, not a data item.

+3
source share
2 answers

Well in some projects, packaging is important. Just stop thinking if later you need to, for example, change the I / O libs files (dunno why you want to do this, since C ++ libs are optimized and ISO). What would you do then? Change all calls from fstream to YourNewSuperMegaLib :: SuperFileSystem :: MegaFileStream? If you want simplicity, I would just inherit fstream from the constructor, pass the opening modes you need and call the super constructor.

+2

Hmya, fstream . fstream, , .

( ) , .

+4

Source: https://habr.com/ru/post/1728780/


All Articles