Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

19.9 Use the Capabilities of the Library!

By far the most common mistake is not to use the full power of the library. If you find yourself writing a little "helper" class, consider why you are doing it. Or, if what you are writing is looking a little clumsy, then maybe there's a more elegant approach. A bit of searching through the Tools.h++ manual may uncover just the thing you're looking for!

Here's a surprisingly common example:

main(int argc, char* argv[]){
   char buf[120];                   //uh oh: possible overflow
   ifstream fstr(argv[1]);
   RWCString line;

   while (fstr.readline(buf,sizeof(buf)) {
     line = buf;                                //hmm: extra copy
     cout << line;
   }
}

This program reads lines from a file specified on the command line and prints them to standard output. By using the full abilities of the RWCString class it could be greatly simplified as follows:

main(int argc, char* argv[]){
   ifstream fstr(argv[1]);
   RWCString line;

   while (line.readLine(fstr)) {
     cout << line;
   }
}

There are countless other such examples. The point is, if it's looking awkward to you, most likely there's a better way!


Previous fileTop of documentContentsIndexNext file
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.