From Jerry Coffin · 1996-04-11 22:55 · #73713 in EMULATE
In reply to containers that contain function pairs by Darin McBride
On (08 Apr 96) Darin McBride wrote to All...
DM> Again, Chris, Jerry, Bob... the purpose here is not to make a complete
DM> class, but to enable me to forget about calling the 'free' function
DM> here. One day I might look into a more 'full-featured' class that
DM> hides everything, but this was more to allow me a slower transition to
DM> full-fledged OOP design. I thought it might give others some ideas...
DM> :-)
Actually, I _like_ it. I do a lot of little utility classes like this.
DM> // the following class simplifies using the DIR* to run through the
DM> // directory entries, including the automatic closing of the directory
DM> // when the variable goes out of scope.
DM> class Dir
DM> {
DM> private:
DM> DIR* m_DIR;
DM> dirent* m_de;
DM> public:
DM> Dir(const char * pszPath) { m_DIR = opendir(pszPath); Next();
DM> }
DM> ~Dir() { closedir(m_DIR); m_DIR = NULL; }
DM> operator DIR*() { return m_DIR; }
DM> operator !() const { return m_DIR == NULL; }
DM> dirent* Current() { return m_de; }
DM> dirent* Next() { m_de = readdir(m_DIR); return m_de; }
DM> };
One thing that _might_ be worth adding would be a conversion to string
(or cchar) that returs the name of the current file. It's trivial to
add, but can save a fair amount of typing. The other obvious thing
would be to add a ctor that accepts a const string. Again, this doesn't
make a huge difference in the idea of the class, but saves having to do
the conversion manually. (In case it isn't apparent by now, nearly
every place I use a string in C++, I use a class. I do my best to have
every other class I write accept strings in place of char *'s. It just
makes life a LOT more convenient...
Later,
Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
* Origin: Point Pointedly Pointless (1:128/77.3)