0

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?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 1
    `static void Reader::read()` solves the problem of having to create `Reader` object. – Robᵩ Aug 10 '12 at 15:22
  • The main overhead is actually the virtual destructor, which is not needed and which introduces a pointer to the VT. Otherwise instantiating the class is only 1 unused byte. – stefaanv Aug 10 '12 at 15:33
  • @Robᵩ up until C++11, static members were apparently deemed inferior to the namespace approach, see http://stackoverflow.com/questions/4977252/why-unnamed-namespace-is-a-superior-alternative-to-static. Thanks for the hint though! – Nico Schlömer Aug 10 '12 at 15:52

1 Answers1

4

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:

namespace Reader {

OutputData reatIt(const std::string& filename);

} // Reader namespace

Implementation file

namespace Reader {

namespace {

void helper1_() { .... }
void helper2_() { .... }
void helper3_() { .... }

} // anonymous namespace

readIt(const std::string& filename) {
  // use helpers here. Client code will never know.
}

} // Reader namespace
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • When splitting the code in `cpp` and `hpp`, another possibility would be to just not declare the helper functions in the `hpp`. Not sure if this is considered good practice though. – Nico Schlömer Aug 10 '12 at 16:31
  • @nico that is what I propose here, so the caller code has no knowledge of the helpers. – juanchopanza Aug 10 '12 at 16:35
  • So what's the anonymous namespace needed for? – Nico Schlömer Aug 10 '12 at 17:27
  • 1
    @Nico it means that the symbols corresponding to the code in the anonymous namespace are not available to users of the object file compiled from the `.cpp` file. The anonymous namespace hides the symbols so one cannot use the functions outside of the `.cpp`. – juanchopanza Aug 11 '12 at 06:57