Say I want some functions to deal with some file, and I was considering 2 options.
1) Create a class like SavedDataHandler that a user could use like this....
// Note that SavedDataHandler has no members. It just has functions that operate on a
// resource ( the file)
SavedDataHandler gameSave;
gameSave.SaveData( arg1, arg2 ); // to save data
gameSave.DeleteSave(); // Delete the save
...
2) Create a namespace of functions
namespace SavedDataHandler {
SaveData( ... ) { ... }
DeleteSave( ... ) { ... }
...
}
that a user would call like
SavedDataHandler::SaveData( arg1, arg 2 );
SavedDataHandler::DeleteSave();
What would be preferred?
P.S. I thought about this when I was thinking about Scott Meyer's recommendation to prefer non-member non-friend functions to member functions. I've run into decisions where I have a function (usually some private member function to help the class do stuff), that could easily be made into a non-member since it doesn't operate on class privates.
However, the function is only used by that one class. Of course, the program might evolve to a point where another class may need it, but I find it hard to find a place for these non-member functions. It's easy when you have a lot of functions with a general purpose, but I find single non-member functions hard to organize into a specific place, and find that leaving it as a member keeps things clean. Any tips regarding this issue?