In C++, I would like to implement a reader function, i.e., method that is only executed once, returns something and then goes away. The reading process is a little complicated, so I thought I best split the thing up into a read(filename) and several helper functions. To make sure they’re not accidentally accessed, I thought I wrap this whole thing in a stateless class and mark those helper functions as private. This, however, creates the code overhead of having to create an instance of the reader class first, and then call read(filename) on it. Alright, so let’s create a helper function creates an instance of said class, calls read(filename), dumps it and returns the read output.
class Reader
{
public:
Reader();
virtual
~Reader();
OutputData
read(const std::string & fileName);
private:
helper1_();
helper2_();
helper3_();
};
OutputData
ReadIt(const std::string & fileName)
{
Reader r();
OutputData out = r.read(fileName);
return out;
}
Hm, that smells a bit over-engineered. What’s your take on this?
That sounds fine, but if all you need is a set of independent helper functions with no state, it may be a better idea to just use free functions and hide them int he implementation. A way to do this is to put all the helpers in an anonymous namespace in the implementation file.
Header file:
Implementation file